views:

301

answers:

2

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 http://stackoverflow.com/questions/2792603/aspnet-mvc-why-is-modelstate-isvalid-false-the-x-field-is-required-when-that)

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

A: 

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.

Nick Craver
small remark: to receive `dataType : 'json'` in the corresponding jQuery.ajax request one should use `datatype : 'json'` as parameter of jqGrid (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options).
Oleg
+1  A: 

First of all I can forward you the my old answer http://stackoverflow.com/questions/2675625/setting-the-content-type-of-requests-performed-by-jquery-jqgrid/2678731#2678731. 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 http://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086 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.

Oleg