post params in ajax
post params in ajax
D
Posts: 9Questions: 1Answers: 0
Is there a way can i post data while doing the ajax post request,
Like below? ,if so can you please point with example
$('#example').dataTable( {
"ajax": {
"url": "url",
"type": "POST",
"data":data
},
"columns": [
{ "data": "first_name" }
]
} );
} );
This discussion has been closed.
Replies
Sure, you should be able to do what you have done (as per
ajax.data
). If that isn't working, please link to a test case showing the issue.Allan
Thanks much Allan.I am able to pass the data as string.
But I am trying to send the json data,
var temp ={} ;
$('#example').dataTable( {
"ajax": { "url": "url", "type": "POST", "data":temp}, "columns": [ { "data": "first_name" } ] } ); } );
but the json is converting as string while reading : "temp=test&type=user".
How can I pass json as data in ajax post request?
Kindly help me.
Is there anything I should be doing to force this to be a json? I tried setting
contentType: "application/json; charset=UTF-8"
but no luck.
I tried processData: false from ajax ,
When I do the normal ajax request like below,the payload sends data like this as expected - {"title":"title"}
var article = new Object();
article.title = "title";
$.ajax({
url: '/test',
type: 'POST',
processData: false,
data: JSON.stringify( article ),
contentType: "application/json"
})
But with Datatable
$('#example').dataTable( {
"ajax": { "url": "url",
"type": "POST",
processData: false,
data: JSON.stringify( article ),
contentType: "application/json},
"columns": [ { "data": "first_name" } ] } );
} );
the payload sends data like - [object Object].It is not converting as string.
Is there way to use "processData: false" with datatable?All I am trying to do is post the json not as request params but in request body.
configure your data key with a function
"data": function(tableAttr) {
return {
rp: tableAttr.length,//每页显示多少页
start: tableAttr.start//现在是第几页
};
},
I tried it and it worked
Thanks strayling ,
I tried your suggestion,
$('#example').dataTable( {
"ajax": { "url": "url",
"type": "POST",
processData: false,
"data": function ( d ) {
return {
"extra_search": "d",
"test" :"test"
} ;
},
contentType: "application/json},
"columns": [ { "data": "first_name" } ] } ); } );
Without processData: false,
In payload i see - extra_search=d&test=test,
with processData: false,
In payload i see - [object Object]
I am expecting to see - { extra_search:d,test:test}.How can I achieve this?
Any suggestions highly appreciated.
You could try:
That will submit the JSON object ion the body of the request, rather than as HTTP parameters.
Allan
Thanks much Allan,Worked well for me!