ChrisArmstrong ChrisArmstrong - 2 years ago 153
Ajax Question

Posting JSON via AJAX results in garbled request payload

Using JS Datatables jQuery plugin, I'm attempting to post JSON data to the server and use the JSON in the response to ultimately populate the datatable. However I've experiencing some strange behavior.

Using this code:

table_config.ajax = {
"type": "POST",
"url": $(location).attr('pathname'),
"data": JSON.stringify({'member_id':2444}),
"dataType": "json",
"contentType": "application/json",
"dataSrc": "results.data"
};


I get a 400 HTTP response and the request payload looks like:

0=%7B&1=%22&2=m&3=e&4=m&5=b&6=e&7=r&8=_&9=i&10=d&11=%22&12=%3A&13=2&14=4&15=4&16=4&17=%7D

What's going on here?

Answer Source

You are stringifying something that is already a string in the data option.

Try:

table_config.ajax = {
            "type": "POST",
            "url": $(location).attr('pathname'),
            "data": { member_id: 2444 },
            "dataType": "json",
            "contentType": "application/json",
            "dataSrc": "results.data"
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download