Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Sencha Touch向ASP.NET MVC发送JSON数据_Json_Asp.net Mvc 3_Sencha Touch - Fatal编程技术网

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

从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

我正在尝试向服务器发送数据。但是我的代码不起作用。有人能指出错误吗

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

谢谢

删除
应用程序/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(发帖){…}嘿,请看这个……试着回答……嘿,请看这个……试着回答。。。