Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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:

[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!

like image 879
Igor Gaponenko Avatar asked Jun 22 '11 15:06

Igor Gaponenko


2 Answers

Remove the application/json request header because you are not sending a JSON encoded request:

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:

[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:

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) { }
});
like image 200
Darin Dimitrov Avatar answered Nov 16 '22 10:11

Darin Dimitrov


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.

This tool is invaluable and I would highly recommend it!

like image 39
Digbyswift Avatar answered Nov 16 '22 10:11

Digbyswift