我的AJAX没有向我的http发布控制器发送数据。
我的控制器:
[Route("api/sendingData")]
public class TestController : ApiController
{
[HttpPost]
public string Post([FromBody] int propertyID)
{
return string.Format("Test");
}
}
我的AJAX:
$.ajax(
{
url: "api/sendingData",
type: "POST",
dataType: 'json',
data: {
'propertyID': '1'
},
success: function (result) {
console.debug(result);
alert(result);
},
error: function (xhr, status, p3, p4) {
console.debug(xhr);
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
alert(err);
}
});
我正在尝试发送propertyID=1
。但是,当我调试控制器时,显示propertyID=0
。
有人知道哪里出问题了吗?
看起来可能很奇怪,但是您只发送一个值,而不是模型,因此stringify是JSON.stringify(value)
var propertyID = 1;
$.ajax({
url: "api/sendingData",
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(propertyID),
success: function (result) {
console.debug(result);
alert(result);
},
error: function (xhr, status, p3, p4) {
console.debug(xhr);
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
alert(err);
}
});
我也删除了dataType json,因为您的操作方法不是返回json,而是一个字符串。现在,我获得了成功。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句