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); } });