Jquery JQGrid breaks when contentType=application/json?

安稳与你 提交于 2020-01-22 16:35:28

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2845494/jquery-jqgrid-breaks-when-contenttype-application-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!