简体   繁体   English

将JSON数据从Sencha Touch发送到ASP.NET MVC

[英]Send JSON data from Sencha Touch to ASP.NET MVC

I'm trying to send data to the server. 我正在尝试将数据发送到服务器。 But my code does not work. 但我的代码不起作用。 Can someone point out a mistake? 有人能指出错误吗?

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

Thanks! 谢谢!

Remove the application/json request header because you are not sending a JSON encoded request: 删除application/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) { }
});

Personally I would recommend having your controller action directly take the Post model instead of having each property as argument and then manually recopying them into a Post object: 我个人建议你的控制器动作直接采用Post模型,而不是将每个属性作为参数,然后手动将它们重新复制到Post对象:

[HttpPost]
public ActionResult SavePost(Post post)
{
    var postRepository = new PostRepository();
    postRepository.Add(post);
    return Json(...);
}

The default model binder will take care of everything. 默认的模型绑定器将处理所有事情。 Now if you wanted to use JSON as request you could use the JSON.stringify method which is natively built into modern web browsers: 现在,如果您想使用JSON作为请求,您可以使用原JSON.stringify内置于现代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) { }
});

I have recently had similar problems with extJS and MVC but I use Charles to determine what requests are actually being sent and what response if any is being returned. 我最近遇到过与extJS和MVC类似的问题,但我使用Charles来确定实际发送的请求以及返回的响应。

This tool is invaluable and I would highly recommend it! 这个工具是非常宝贵的,我强烈推荐它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM