0
請溫和我一下。我不是專家。ajax post .net web api參數爲空
進出口張貼有效的JSON(驗證)
{
"Stats": [{
"userName": "sadf",
"id": "128",
"score": "7"
}, {
"userName": "fa",
"id": "417",
"score": "3"
}]
}
// POST api/comment
public void Post(List<Stat> Stats)
{
string break= "Breakpoint here";
}
public class Stat
{
public string userName { get; set; }
public string id { get; set; }
public string score { get; set; }
}
不管是什麼,然後我只得到統計==在帖子(列表統計)空
這是JavaScript代碼:
var stats = [{ "userName": "sadf" },
{ "userName": "fa" }];
$.ajax({
type: "POST",
url: "http://localhost:56887/api/comment",
data: JSON.stringify({ Stats: stats }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$.each(stats, function (k, v) {
alert(v.position);
});
},
failure: function (errMsg) {
alert(errMsg);
}
});
任何想法有什麼不對?
您需要將[FromBody]屬性添加到該方法。看到這個答案http://stackoverflow.com/a/10986030/3279876 – Sami
我已經嘗試過,沒有結果。 –
在這種情況下,顯示您的整個ajax郵政編碼。 – Sami