$.ajax({
url: '/api/search',
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
data: '=' + encodeURIComponent(request.term),
success: function (data) {
response(data.d);
},
error: function (result) {
alert('Error');
}
});
基本上你只能有一个标量类型的参数,该参数用[FromBody]
属性和您的请求需要使用application/x-www-form-urlencoded
POST 有效负载应如下所示:
=somevalue
请注意,与标准协议相反,缺少参数名称。您仅发送值。
您可以阅读有关 Web Api 中的模型绑定如何工作的更多信息this article http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1.
但当然,这种乱搞行为是一件病态的事情。您应该使用视图模型:
public class MyViewModel
{
public string Value { get; set; }
}
然后摆脱[FromBody]
属性:
public IEnumerable<string> Post(MyViewModel model)
{
return new string[] { "value1", "value2", model.Value };
}
然后使用 JSON 请求:
$.ajax({
url: '/api/search',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ value: request.term }),
success: function (data) {
response(data.d);
},
error: function (result) {
alert('Error');
}
});