2013-12-18 106 views
1

我有一个简单的ajax调用,它将一个json字符串传递给控制器​​动作,并且如果json的内容部分太长,或者json一般情况下,服务器返回一个404,如果我缩短了内容,它的请求会解析并正确完成。对ASP.NET MVC控制器的Ajax调用当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#的控制器动作

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); 
    } 
+0

还有一种是[最大长度查询字符串] [1] 难道你们就不能使用PUT或帖子? [1]:http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string –

回答

0

您是否尝试过使用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'); 
+0

这样做的伎俩......谢谢,我我不敢相信我没有想到这一点。我需要喝点咖啡。 –

0

试试这个在web配置

<system.web.extensions> 
<scripting> 
    <webServices> 
    <jsonSerialization maxJsonLength="500000000"/> 
    </webServices> 
</scripting></system.web.extensions> 
相关问题