【WEBAPI】关于WEBAPI无法获取数据的问题之一(Issues while making a POST to a Web API from JQuery)
2013-02-25 10:12 朱峰(Peter.zhu) 阅读(435) 评论(0) 收藏 举报I have a web api with the following POST Method
public HttpResponseMessage Post([FromBody]string package)
I have a console app that uses the HttpCLient with no problems. When I try to make a call by means of jQuery, I get null on the package variable.
This is the code I have right now:
$.ajax({
url: 'http://localhost:8081/api/Package/',
type: 'POST',
data: JSON.stringify(message),
contentType: "application/json;charset=utf-8",
success: function (data) {
alert(data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
}
});
直接采用上面的代码,无法获取数据,在参数前加个 "=",就可以映射到服务参数中去。如下代码
$.ajax({
url: 'http://localhost:8081/api/Package/',
type: 'POST',
data: "=" + JSON.stringify(message),
contentType: "application/json;charset=utf-8",
success: function (data) {
alert(data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
}
});
浙公网安备 33010602011771号