gpt4 book ai didi

json - 将Sencha Touch的JSON数据发送到ASP.NET MVC

转载 作者:行者123 更新时间:2023-12-04 13:43:37 26 4
gpt4 key购买 nike

我正在尝试将数据发送到服务器。但是我的代码不起作用。有人可以指出一个错误吗?

Sencha代码:

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();
}

谢谢!

最佳答案

删除application/json请求 header ,因为您没有发送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) { }
});

就个人而言,我建议让您的 Controller 操作直接采用 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) { }
});

关于json - 将Sencha Touch的JSON数据发送到ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com