POSTing JSON with built in ajax functionality.
POSTing JSON with built in ajax functionality.
Steven_Crawford
Posts: 3Questions: 1Answers: 0
I'm trying to set up my table as follows
var UserTable = jQuery('#UserListTable').DataTable( {
"ajax":{
"url" : "/getActiveUsers",
"type" : "POST",
"contentType": "application/json",
"data": JSON.stringify({"applications" : ["sca","www"]})
},...
When I do this my data is being sent as follows
The data in the HTTP request payload looks like this:
0=%7B&1=%22&2=a&3=p&4=p&5=l&6=i&7=c&8=a&9=t&10=i&11=o&12=n&13=s&14=%22&15=%3A&16=%5B&17=%22&18=s&19=c&20=a&21=%22&22=%2C&23=%22&24=w&25=w&26=w&27=%22&28=%5D&29=%7D
I can manually pass the same object into jquery.ajax and the request works as expected.
jQuery.ajax({
"url" : "/getActiveUsers",
"type" : "POST",
"contentType": "application/json",
"data": JSON.stringify({"applications" : ["sca","www"]})
});
Any idea what is going on here?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Try it like this. I don't know why you would want to stringify your object, but I am also not sure what you are doing.
Thanks ignignokt,
I was stringifying the object as I've read elsewhere that may be a factor. I've also tried passing in a plain object.
Anyway, I don't want to change the server, I just want the post to be plain JSON, but with this it's converted to a form.
@ignignokt's solution looks exceptionally close! There the data object is being modified and submitted to the server using jQuery's standard methods.
But if you want to submit the JSON in the request body (as it sounds like) you can use
ajax.data
to return a string:If you were using server-side processing, you would extend the
d
parameter passed in with your custom options and then return a string version of the whole lot.Allan
Awesome, That worked. Thanks Allan!