jQWidgets Forums
jQuery UI Widgets › Forums › Grid › POST not sending JSON!
This topic contains 2 replies, has 2 voices, and was last updated by RG 10 years, 4 months ago.
-
AuthorPOST not sending JSON! Posts
-
Hi,
I am trying to send a POST request which will post JSON to a WebAPI which will then display the results (JSON) in my grid.
This is what I have but it refuses to send data in JSON format. It is ALWAYS sending a querystring and i’ve spent days on this!
This is the data being sent:
Request URL:http://localhost:49376/api/mainGrid Request Headers CAUTION: Provisional headers are shown. Accept:application/json, text/javascript, */*; q=0.01 Content-Type:application/json; charset=UTF-8 Origin:http://localhost:49376 Referer:http://localhost:49376/dashboard.aspx User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36 X-Requested-With:XMLHttpRequest Request Payload filterscount=0&groupscount=0&pagenum=0&pagesize=10&recordstartindex=0&recordendindex=10&group=%5B%5D&staff=%5B%5D&MODULE=18
And this is my code:
var url = "api/mainGrid"; var source = { dataType: 'json', contentType: 'application/json; charset=UTF-8', url: url, processData: false, type: "POST", id: "SEQUENCE", root: 'rowsinfo', cache: false, columns: [], datafields: [], beforeprocessing: function (data) { var columnsdata = new Array(); var datafieldsdata = new Array(); for (k in data.columnsinfo) { var col = {}; col.text = data.columnsinfo[k]["DISPLAYNAME"]; col.datafield = data.columnsinfo[k]["DISPLAYNAME"]; var datafields = {}; datafields.name = data.columnsinfo[k]["DISPLAYNAME"]; columnsdata.push(col); datafieldsdata.push(datafields); source.columns = columnsdata; source.datafields = datafieldsdata; } $("#jqxgrid").jqxGrid({ columns: source.columns }); }, data: {group: JSON.stringify(checkedGroups), staff: JSON.stringify(checkedStaff), MODULE: JSON.stringify(selectedModuleSEQ)} }; var dataAdapter = new $.jqx.dataAdapter(source, { cache: false, formatData: function (data) { $.extend(data, { group: JSON.stringify(checkedGroups), staff: JSON.stringify(checkedStaff), MODULE: selectedModuleSEQ }); return data; }
I’ve tried everything and every format of using JSON.stringify and I cannot work out why it is not sending JSON.
Any ideas would be great.
Thanks
I have finally sorted this, I just needed to put JSON.stringify around the return like:
return JSON.stringify(data);
and remove the data parameter from the source.
Thanks a bunch for this. When I first read it I dismissed it thinking you removed the data statement completely. After wrestling with this problem I see it is solved with your solution using formatData … so I prefer to use your solution this way:
contentType: ‘application/json’,
formatData: function (data) {
return JSON.stringify(data);
},
var params = new Object()…. and in the source definition…
var source = {
datatype: “json”,
type: “POST”,
data:params -
AuthorPosts
You must be logged in to reply to this topic.