javascript - 当 Json 长度太长时,Ajax 调用 ASP.NET MVC Controller 返回 404

标签 javascript ajax asp.net-mvc json c#-4.0

我有一个简单的ajax调用,它将json字符串传递给 Controller ​​操作,如果json的内容部分太长,或者一般的json字符串太长,服务器会返回404,如果我缩短内容,如果请求正确解析并完成。

我以为这是微软 JavaScriptSeralizer 的 8k 限制,但我更新了 MaxJsonLength,但没有成功。有人可以告诉我这是怎么回事吗?

这是我的 ajax 请求(注意:这是使用 Knockout.js)

 self.updatePost = function () {
            var postToUpdate = ko.toJS(self.selectedPost);
            postToUpdate.Content = $("#wmd-input").val();
            console.log(postToUpdate);

            $.getJSON('/blogs/posts/update', {post: ko.toJSON(postToUpdate)}, function(post) {
                if (post) {
                    // remove the selected post and add the updated post
                    self.posts.remove(self.selectedPost());
                    var updatedPost = new Post(post);
                    self.posts.unshift(updatedPost);
                    self.selectedPost(updatedPost);
                    $("#ghost-list li:first").trigger('click');
                    // show alert
                }
            });
        };

C# Controller 操作

 public JsonResult Update(string post)
    {
        var seralizer            = new JavaScriptSerializer();
        seralizer.MaxJsonLength  = int.MaxValue;
        seralizer.RecursionLimit = 100;
        var selectedPost         = seralizer.Deserialize<Post>(post);
        var student              = students.GetStudentByEmail(User.Identity.Name);
        var blog                 = db.Blogs.SingleOrDefault(b => b.StudentID == student.StudentID);
        var postToUpdate         = blog.BlogPosts.SingleOrDefault(p => p.ID == selectedPost.ID);

        if (postToUpdate != null)
        {
            // update the post fields
            postToUpdate.Title       = selectedPost.Title;
            postToUpdate.Slug        = BlogHelper.Slugify(selectedPost.Title);
            postToUpdate.Content     = selectedPost.Content;
            postToUpdate.Category    = selectedPost.Category;
            postToUpdate.Tags        = selectedPost.Tags;
            postToUpdate.LastUpdated = DateTime.Now;
            if (selectedPost.Published)
            {
                postToUpdate.DatePublished = DateTime.Now;
            }
            // save changes
            db.SaveChanges();

            var jsonResult = Json(seralizer.Serialize(selectedPost), JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }
        return Json(false, JsonRequestBehavior.AllowGet);
    }

最佳答案

您是否尝试过使用post方法:

$.post('/blogs/posts/update', {post: ko.toJSON(postToUpdate)}, function(post) {
    if (post) {
        // remove the selected post and add the updated post
        self.posts.remove(self.selectedPost());
        var updatedPost = new Post(post);
        self.posts.unshift(updatedPost);
        self.selectedPost(updatedPost);
        $("#ghost-list li:first").trigger('click');
        // show alert
    }
 }, 'json');

关于javascript - 当 Json 长度太长时,Ajax 调用 ASP.NET MVC Controller 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654601/

相关文章:

javascript - 使用/和/:slug as different route files 表示动态路由

c# - ms ajax $find ('clientID' ) 不断出现错误

javascript - Rails渲染JS部分打印代码到页面

c# - ASP.NET MVC : 'Extend' Html. TextBox 有条件地具有 CSS 值?

Javascript strtotime 格式化非特定日期

javascript - 在验证后将焦点设置到文本框表单元素

javascript - 改变Javascript动画

jquery ajax意外 token :

c# - Razor View 看不到外部组件

javascript - 拒绝加载脚本,因为它违反了以下内容安全策略指令 : "style-src ' self' 'unsafe-inline'