Jquery JQGrid breaks when contentType=application/json?

I've had to use $.ajaxSetup() to globally change the contentType to application/json

$.ajaxSetup({
  contentType: "application/json; charset=utf-8"
});

(See this question for why I had to use application/json ASPNET MVC - Why is ModelState.IsValid false "The x field is required" when that field does have a value?)

But this breaks the jquery jqrid with this error:

Invalid JSON primitive: _search

The POST data it is trying to send is:

_search=false&nd=1274042681880&rows=20&page=1&sidx=&sord=asc

Which of is not in json format, so of course it fails. Is there anyway to tell jqrid what contenttype to use?

I have searched on the jqrid wiki, but doesn't have much documentation about anything really.

http://www.trirand.com/jqgridwiki/doku.php?do=search&id=contenttype&fulltext=Search


First of all I can forward you the my old answer Setting the content-type of requests performed by jQuery jqGrid. It shows how the ajax request looks like inside of jqGrid. So you should use ajaxGridOptions parameter of jqGrid instead of overwriting global settings with respect of $.ajaxSetup .

Moreover in the same answer you can see how you serializeGridData parameter of jqGrid can be used to make your custom serialization. In How do I build a JSON object to send to an AJAX WebService? you can read how should JSON encoding of parameters look like.

If you will stay have problem with using serializeGridData and ajaxGridOptions you should include in your question the code fragment of using jqGrid and the prototype of your server's method of the web service which you use.


When you're setting up jqGrid or it's datasource, set it's dataType to JSON ( "json" ), like this:

$("#myTable").jqGrid ({
  //other options...
  dataType : 'json'
});

You can see an example on code project as well.

链接地址: http://www.djcxy.com/p/46172.html

上一篇: 如何延迟.keyup()处理程序,直到用户停止键入?

下一篇: jquery JQGrid在contentType = application / json时中断了吗?