我有一個簡單的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);
}
還有一種是[最大長度查詢字符串] [1] 難道你們就不能使用PUT或帖子? [1]:http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string –