jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Invalid JSON primitive
Tagged: dataadapter, jqxgrid, json, source
This topic contains 2 replies, has 2 voices, and was last updated by Peter Stoev 10 years, 5 months ago.
-
AuthorInvalid JSON primitive Posts
-
Hello all
I have been trying to populate a jqxGrid with data coming from a web method. This web method receives two parameters, in order to select the data from the database, and then serializes the data to return a JSON string. I set the data property formatted as a JSON string, but the following error arises:
{“Message”:”Invalid JSON primitive: %7B\u00261=u\u00262=s\u00263=e\u00264=r\u00265=n\u00266=a\u00267=m\u00268=e\u00269=%3A\u002610=%22\u002611=U\u002612=s\u002613=e\u002614=r\u002615=+\u002616=2\u002617 … “}
It seems that my parameters are being double-serialized somewhere else, but I can’t find any place in my code doing that.
This is the web method that’s working:
[WebMethod] public string getUserOptions(string username, int menuId) { DataTable dt = item.getUserOptions(username, menuId); return JsonConvert.SerializeObject(dt); }
By using a simple $.ajax() call to the web method, everything works perfectly and I get the expected results:
jQuery.ajax({ type: 'post', url: '/WebService1.asmx/getUserOptions', dataType: 'json', contentType: 'application/json', data: '{username:"' + val1 + '", menuId:"' + $("#menu_id").val() + '"}', success: function(response) { var data = response.d; var source = { datatype: 'json', datafields: [ { name: 'menu_id' }, { name: 'option_id' }, { name: 'caption' }, { name: 'form_to_open' }, { name: 'item_order' } ], localdata: data }; var dataAdapter = new $.jqx.dataAdapter(source); $("#grid").jqxGrid({ source: dataAdapter, ... });
Microsoft framework 4.0 is supposed to serialize the response for me in a web method, so I should return a DataTable object instead of a string (using $.ajax() call) but if I do so I get the error: “A circular reference was detected while serializing an object of type \u0027System.Reflection.RuntimeModule\u0027.”
This web method does not work for me:
[WebMethod] public DataTable getUserOptions(string username, int menuId) { DataTable dt = item.getUserOptions(username, menuId); return dt; }
Calling this web method from the jqxGrid source will raise the error “Invalid JSON primitive” again. So that’s why decided to change it to return a string.
This is my jqxGrid code:
var val1 = 'User 2'; var source = { type: 'post', url: '/WebService1.asmx/getUserOptions', datatype: 'json', contentType: 'application/json', data: '{username:"' + val1 + '", menuId:"' + $("#menu_id").val() + '"}', datafields: [ { name: 'menu_id' }, { name: 'option_id' }, { name: 'caption' }, { name: 'form_to_open' }, { name: 'item_order' } ] }; // load items of the selected menu for the current user. var dataAdapter = new $.jqx.dataAdapter(source); $("#grid").jqxGrid({ source: dataAdapter, ... });
As you can see on both techniques I am using exactly the same AJAX configuration, calling the working web method.
Why isn’t it working for jqxGrid object?Can anybody put me on the right track to solve this issue?
And yes… I already read this:
as well as the encosia.com articles regarding this issue, but I can’t make it work yet.
Hello jgarcias,
If you wish, you can prepare and share jsfiddle.net sample which shows an issue with loading your JSON data in jqxGrid. “Invalid JSON primitive” is not an error raised by our framework.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com/ -
AuthorPosts
You must be logged in to reply to this topic.