这是我的客户端AJAX调用:
var list = ["a", "b", "c", "d"];
var jsonText = { data: list };
$.ajax({
type: "POST",
url: "/api/scheduledItemPriceStatus/updateStatusToDelete",
data: jsonText,
dataType: "json",
traditional: true,
success: function() { alert("it worked!"); },
failure: function() { alert("not working..."); }
});
这是铬合金网头:
Request URL:http://localhost:2538/api/scheduledItemPriceStatus/updateStatusToDelete
Request Method:POST
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:27
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:2538
Origin:http://localhost:2538
Referer:http://localhost:2538/Pricing/ScheduledItemPrices
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
X-Requested-With:XMLHttpRequest
表格数据视图的URL连接codeD
Form Dataview URL encoded
data:a
data:b
data:c
data:d
这是我的WebAPI控制器的方法:
public HttpResponseMessage UpdateStatusToDelete(string[] data)
结果:
当我调试,在UpdateStatusToDelete数据参数返回 {字符串[0]}
,而不是数据:一
数据:乙
数据:C
数据:D
when I debug, the data parameter in UpdateStatusToDelete returns {string[0]}
instead of data:a
data:b
data:c
data:d
我是什么做错了吗?任何帮助真的AP preciated。
What am I doing wrong? Any help is really appreciated.
有关通过简单的类型,数据发布必须采取一个名字值对的名称部分为空字符串的形式。所以,你需要做的Ajax调用就像这样:
For passing simply types, the data to post must take the form of a name value pair with the name portion being an empty string. So you need to make the Ajax call like so:
$.ajax({
type: "POST",
url: "/api/values",
data: { "": list },
dataType: "json",
success: function() { alert("it worked!"); },
failure: function() { alert("not working..."); }
});
此外,您的Web API的行动,将其标注为瓦特/ [FromBody]属性。是这样的:
Additionally, on your Web API action, annotate it w/ the [FromBody] attribute. Something like:
public void Post([FromBody]string[] values)
这是应该做的伎俩。