从Sencha Touch向ASP.NET MVC发送JSON数据
我正在尝试向服务器发送数据。但是我的代码不起作用。有人能指出错误吗 Sencha代码:从Sencha Touch向ASP.NET MVC发送JSON数据,json,asp.net-mvc-3,sencha-touch,Json,Asp.net Mvc 3,Sencha Touch,我正在尝试向服务器发送数据。但是我的代码不起作用。有人能指出错误吗 Sencha代码: Ext.Ajax.request({ url: '/Blog/SavePost', method: 'POST', headers: { 'Content-Type': 'application/json;cha
Ext.Ajax.request({
url: '/Blog/SavePost',
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
params: {
id: currentPost.data.Id,
title: currentPost.data.Title,
text: currentPost.data.Text,
authorName: currentPost.data.AuthorName,
authorEmail: currentPost.data.AuthorEmail,
postDate: currentPost.data.PostDate
},
failure: function (response) { },
success: function (response, opts) { }
});
MVC代码:
[HttpPost]
public ActionResult SavePost(int id, string title, string text, string authorName, string authorEmail, DateTime postDate)
{
Post post = new Post { Id = id, Title = title, Text = text, AuthorEmail = authorEmail, AuthorName = authorName, PostDate = postDate };
var postRepository = new PostRepository();
postRepository.Add(post);
return Json();
}
谢谢 删除
应用程序/json
请求头,因为您没有发送json编码的请求:
Ext.Ajax.request({
url: '/Blog/SavePost',
method: 'POST',
params: {
id: currentPost.data.Id,
title: currentPost.data.Title,
text: currentPost.data.Text,
authorName: currentPost.data.AuthorName,
authorEmail: currentPost.data.AuthorEmail,
postDate: currentPost.data.PostDate
},
failure: function (response) { },
success: function (response, opts) { }
});
就我个人而言,我建议您的控制器操作直接采用Post
模型,而不是将每个属性作为参数,然后手动将它们重新压缩到Post对象中:
[HttpPost]
public ActionResult SavePost(Post post)
{
var postRepository = new PostRepository();
postRepository.Add(post);
return Json(...);
}
默认的模型绑定器将处理所有内容。现在,如果您想使用JSON作为请求,您可以使用现代web浏览器内置的JSON.stringify
方法:
Ext.Ajax.request({
url: '/Blog/SavePost',
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
params: {
post: JSON.stringify({
id: currentPost.data.Id,
title: currentPost.data.Title,
text: currentPost.data.Text,
authorName: currentPost.data.AuthorName,
authorEmail: currentPost.data.AuthorEmail,
postDate: currentPost.data.PostDate
})
},
failure: function (response) { },
success: function (response, opts) { }
});
我最近在extJS和MVC上遇到了类似的问题,但我使用它来确定实际发送的是什么请求以及返回的是什么响应
这个工具非常宝贵,我强烈推荐它 你能详细说明什么不起作用吗?谢谢!它起作用了但如果我在params部分使用'post:JSON.stringify',则'post'将为null。结果:对于发送数据参数:{id:currentPost.data.id,title:currentPost.data.title,text:currentPost.data.text,authorName:currentPost.data.authorName,authorEmail:currentPost.data.authorEmail,postDate:currentPost.data.postDate},对于接收数据公共操作结果SavePost(发帖){…}嘿,请看这个……试着回答……嘿,请看这个……试着回答。。。