1

I have a simple ajax call which is passing a json string to a controller action and if the content portion of the json is too long, or the json string in general, the server returns a 404, if I shorten the content, it the request resolves and completes correctly.

I thought it was do to the 8k limit of Microsoft's JavaScriptSeralizer, but I have updated the MaxJsonLength, with no luck. Can somebody please tell me what's going on here?

Here is my ajax request (Note: This is using 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
                }
            });
        };

The C# Controller Action

 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);
    }
Carl Weis
  • 6,794
  • 15
  • 63
  • 86
  • There kind of is a [maximum querystring length][1] Can you not use a PUT or a POST? [1]: http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string – Neil Thompson Dec 18 '13 at 09:53

2 Answers2

0

Have you tried using the post method:

$.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');
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Try this at web config

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="500000000"/>
  </webServices>
</scripting></system.web.extensions>
Hitesh Gaur
  • 362
  • 1
  • 11