首先编写视图模型:
public class Myviewmodel
{
public string Token { get; set; }
public Feed Feed { get; set; }
}
您的控制器操作将作为参数:
[HttpPost]
public HttpResponseMessage Post(Myviewmodel model)
{
/* Some code */
return new HttpResponseMessage(HttpStatusCode.Created);
}
最后调整你的jQuery调用将其作为JSON发送:
$.ajax({
url: url,type: 'POST',contentType: 'application/json',data: JSON.stringify({
token: '4',Feed: {
id: 0,message: 'Hello World',userId: 4
}
})
});
AJAX调用需要注意的重要事项:
>将请求contentType设置为application / json>将数据包装在JSON.stringify函数中,以有效地将javascript对象转换为JSON字符串>删除无用的dataType:’json’参数. jQuery将自动使用服务器发送的Content-Type响应头来推断如何解析传递给成功回调的结果.