post params in ajax

post params in ajax

DD 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" }
]
} );
} );

Replies

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    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

  • DD Posts: 9Questions: 1Answers: 0

    Thanks much Allan.I am able to pass the data as string.
    But I am trying to send the json data,

    var temp ={} ;

    temp["test"] = "test";
    temp["type"] = "user";
    

    $('#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.

  • DD Posts: 9Questions: 1Answers: 0

    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.

  • DD Posts: 9Questions: 1Answers: 0

    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.

  • straylingstrayling Posts: 10Questions: 1Answers: 0

    configure your data key with a function
    "data": function(tableAttr) {
    return {
    rp: tableAttr.length,//每页显示多少页
    start: tableAttr.start//现在是第几页
    };
    },
    I tried it and it worked

  • DD Posts: 9Questions: 1Answers: 0

    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.

  • allanallan Posts: 61,892Questions: 1Answers: 10,144 Site admin

    You could try:

    data: function ( json ) {
      return JSON.stringify( json );
    }
    

    That will submit the JSON object ion the body of the request, rather than as HTTP parameters.

    Allan

  • DD Posts: 9Questions: 1Answers: 0

    Thanks much Allan,Worked well for me!

This discussion has been closed.