【发布时间】:2017-02-13 14:34:56
【问题描述】:
请对我温柔一点。我不是专家。
我发布了一个有效的 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; }
}
无论如何,那我只能在 Post(List Stats) 中得到 Stats == null
这是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] 属性。看到这个答案stackoverflow.com/a/10986030/3279876
-
我已经试过了,没有结果。
-
在这种情况下显示您的整个 ajax 邮政编码。
-
@sami 刚刚用 javascript 更新
-
尝试将数据:JSON.stringify({ Stats: stats }) 更改为数据:JSON.stringify(stats)
标签: asp.net json asp.net-web-api