Json ApiController在POST中接收空值
你会想:这有多难?嗯,似乎是这样 jQuery正在运行:Json ApiController在POST中接收空值,json,webforms,asp.net-apicontroller,Json,Webforms,Asp.net Apicontroller,你会想:这有多难?嗯,似乎是这样 jQuery正在运行: $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "/api/TEST", data: {"":{close:true, user: "<%: uuid %>" }}, success: nu
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/api/TEST",
data: {"":{close:true, user: "<%: uuid %>" }},
success: null,
dataType: "json"
});
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“/api/TEST”,
数据:{“”:{close:true,用户:'}},
成功:空,
数据类型:“json”
});
我正在使用asp.NETWebForms和ApiContoller,试图发布一些简单的数据
public class TESTController : ApiController {
// GET api/<controller>
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id) {
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value) {
string k = ";;";
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value) {
}
// DELETE api/<controller>/5
public void Delete(int id) {
}
}
公共类TESTController:ApiController{
//获取api/
公共IEnumerable Get(){
返回新字符串[]{“value1”,“value2”};
}
//获取api//5
公共字符串Get(int-id){
返回“值”;
}
//后api/
公共作废帖子([FromBody]字符串值){
字符串k=“;;”;
}
//放置api//5
公共void Put(int id,[FromBody]字符串值){
}
//删除api//5
公共无效删除(int-id){
}
}
调用POST方法,但该值始终为null。我尝试过在ajax帖子中更改数据,但之前没有空引号,没有任何效果。显然,在浏览了几个小时的博客之后,在上面的示例中,这就是您需要做/编辑的全部工作:
public string Post(tester test) {
string k = ";;";
return k;
}
添加一个类
public class tester {
public string user { get; set; }
public bool close { get; set; }
}
并将AJAX更改为
var value = {close:true, user: "<%: uuid %>" };
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/api/TEST/",
data: JSON.stringify(value)
}).done(function(msg) {
alert("done"+msg);
});
var值={close:true,用户:'};
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“/api/TEST/”,
数据:JSON.stringify(值)
}).done(函数(msg){
警报(“完成”+消息);
});