你的位置:首页 > 软件开发 > ASP.net > Web Api中的get传值和post传值

Web Api中的get传值和post传值

发布时间:2015-11-09 15:00:06
GET 方式get方式传参 我们一般用于获取数据做条件筛选,也就是 “查”1.无参var look = function () { $.ajax({ type: "GET", url: &quo ...

GET 方式

get方式传参 我们一般用于获取数据做条件筛选,也就是 “查”

1.无参

Web Api中的get传值和post传值
var look = function () {    $.ajax({      type: "GET",      url: "http://172.28.20.106:8002/api/users/",      data:{"id":2},      dataType: "",      contentType: 'application/'    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });  }
  var look = function () {    $.ajax({      type: "GET",      url: "http://172.28.20.106:8002/api/users/2",      dataType: "",      contentType: 'application/'    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });  }

输出:<string id:2</string>

Web Api中的get传值和post传值

3.多个简单参数

后台方法体如下:
  var look = function () {    $.ajax({      type: "GET",      url: "http://172.28.20.106:8002/api/users",       data:{"id":2,"name":"张飞"},      dataType: "json",      contentType: 'application/json;charset=gb2312;'    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });  } 
var look = function () {    var Product = {      Id: 1,      Name: "张飞",      Category:"212",      Price:22    };    $.ajax({      type: "GET",      url: "http://172.28.20.106:8002/api/users/Manger",      //data:{ "model":{"Id":2,"Name":"张飞","Category":"22","Price":22}},      data: Product ,      dataType: "json",      contentType: 'application/json;charset=gb2312;'    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });  }
[Route("Manger")]public string Get([FromUri] Product model){return "id:" + model.Id + " name:" + model.Name;}输出:"id:1 name:张飞"
Web Api中的get传值和post传值

 

POST方式

   post方式我们一般用来做增 删 改 ,不过在web api中post仅仅用来做增加操作  修改用put 删除用delete

可以看看模板给我们生成的东西

Web Api中的get传值和post传值
    $.ajax({      type: "POST",      url: "http://172.28.20.106:8002/api/users/addid",       data: {"value":"1"}    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });
Web Api中的get传值和post传值

结果输出却发现   值是:"

后台并没有收到数据

总结:

  当只有一个参数时,有2种方式是可以获取到值的

 1. data: {"":"1"} //忽略参数名
 2. data: "=1"  //加上”=“号 并且去掉花括号
Web Api中的get传值和post传值
  $.ajax({      type: "POST",      url: "http://172.28.20.106:8002/api/users/addid",       data: "=1"    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });
Web Api中的get传值和post传值

输出:"值是:1"

Web Api中的get传值和post传值Web Api中的get传值和post传值

2.post的多个参数(1个对象)

   注意:post并不能提交多个参数,只能合并成对象

  错误写法:

Web Api中的get传值和post传值
 public string Post(int id,[FromBody] string name)     {       return "id是:" + id+" name:"+name;     }
   var Product = {      Id: 1,      Name: "张飞",      Category: "212",      Price: 22          };    $.ajax({      type: "POST",      url: "http://172.28.20.106:8002/api/users/addmodel",      data: Product    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });
Web Api中的get传值和post传值

输出:  "值是:1 name :张飞"

 

3.post的多个参数(多个对象)

    如果有多个对象,那就只能把它封装在一个扩展对象里面了

   

Web Api中的get传值和post传值
  var Product = {      Id: 1,      Name: "手机",      Category: "212",      Price: 22          };    var User = {      Id: 1,      Name: "张飞",    };    $.ajax({      type: "POST",      url: "http://172.28.20.106:8002/api/users/addnewmodel",       data: { Product: Product ,User:User}    }).success(function (res) {      console.log(res);    }).error(function (xhr, status) {      console.log(xhr);    });
Web Api中的get传值和post传值

输出: "userid值是:1 priduct值是 :手机"

 

也可以写成这样

Web Api中的get传值和post传值使用步骤:

原标题:Web Api中的get传值和post传值

关键词:web

web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。