欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ajax调用WebAPI添加数据

程序员文章站 2022-07-11 10:12:26
API后台 WebApi FromBody参数 查看 https://blog.csdn.net/bafnypeu/article/details/78262684 ......

//获取账号名 var name = document.getelementbyid("text1").value;//获取密码 var pass = document.getelementbyid("password1").value; //获取真实姓名 var realname = document.getelementbyid("text3").value; //获取联系电话 var telphone = document.getelementbyid("text4").value; //获取用户状态 var status = 0; //判断单选框是否选中 var val = $("input:radio[name='qi']:checked").val(); if (val == null) { alert("您没有选择状态"); } else { //创建一个类 var userinfo = { name: name, pass: pass, realname: realname, telphone: telphone, status: parseint(val) } //调用方法 $.ajax({ url: "api地址", type: "post",
//将类转换为json字符串 data: json.stringify(userinfo), datatype: "json", contenttype: "application/json;charset=utf-8", success: function (data) { if (data > 0) { alert("添加成功"); } else { alert("添加失败"); } } }) }

 api后台

//api代码    
[httppost] public int post([frombody]userinfo userinfo) { return bll.adduser(userinfo); }

 

webapi frombody参数

查看