使用jQuery发布到ASP.Net webapi
遇到麻烦:
我做了这个简单的测试,警报会弹出文本“test return simple”:
jQuerypost:
$.post("http://www.localhost/webapi/api/corkboard/test/", jsonData) .done(function(data){ alert(data); });
Asp.Net WebAPI:
[HttpPost] public string test() { return "test return simple"; }
但是当我通过添加参数来更改WebAPI时:
public string test(string JSONData) { var jData = Json.Decode(JSONData); return "test return: " + jData.Filter; }
我收到以下错误消息:
“没有找到与请求URI匹配的HTTP资源’ http://www.localhost/webapi/api/corkboard/test/ ‘
坚持并会感激任何想法…谢谢!
将WebApi方法更改为:
public string test([FromBody]string JSONData) { var jData = Json.Decode(JSONData); return "test return: " + jData.Filter; }
和你的JQuery:
$.post('http://www.localhost/webapi/api/corkboard/test/', { '': jsonData }) .done(function(data){ alert(data); });
请尝试以下代码..
$.post("http://www.localhost/webapi/api/corkboard/test/", { value: jsonData }) .done(function(data){ alert(data); });
或者,您可以查看以下链接..
http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/