当我用下面的ajax调用下面的Post方法时,为什么参数总是为null?
public IEnumerable<string> Post([FromBody]string value)
{
return new string[] { "value1", "value2", value };
}
Run Code Online (Sandbox Code Playgroud)
这是通过ajax调用Web API方法:
function SearchText() {
$("#txtSearch").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "api/search/",
data: "test",
dataType: "text",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
Run Code Online (Sandbox Code Playgroud) 遇到麻烦:
我做了这个简单的测试,警报会弹出文本"test return simple":
jQuery帖子:
$.post("http://www.localhost/webapi/api/corkboard/test/", jsonData)
.done(function(data){
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
Asp.Net WebAPI:
[HttpPost]
public string test()
{
return "test return simple";
}
Run Code Online (Sandbox Code Playgroud)
但是当我通过添加参数来更改WebAPI时:
public string test(string JSONData)
{
var jData = Json.Decode(JSONData);
return "test return: " + jData.Filter;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
"没有找到与请求URI匹配的HTTP资源' http://www.localhost/webapi/api/corkboard/test/ '
坚持并会感激任何想法...谢谢!