POSTing JSON with built in ajax functionality.

POSTing JSON with built in ajax functionality.

Steven_CrawfordSteven_Crawford Posts: 3Questions: 1Answers: 0
edited February 2015 in Free community support

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

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited February 2015

    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.

    var UserTable = jQuery('#UserListTable').DataTable( {
        "ajax":{
            "url" : "/getActiveUsers",
            "type" : "POST",
            data:function(d){
                d.applications = {"applications" : ["sca","www"]};
            }
        },...
    
    print_r($_POST['applications']);
    
    Array
    (
        [applications] => Array
            (
                [0] => sca
                [1] => www
            )
    
    )
    
  • Steven_CrawfordSteven_Crawford Posts: 3Questions: 1Answers: 0

    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.

    Post Content:
    applications[applications][]:sca
    applications[applications][]:www
    
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    edited February 2015 Answer ✓

    @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:

    ajax: {
      url: ...,
      data: function ( d ) {
        return JSON.stringify({"applications" : ["sca","www"]});
      }
    }
    

    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

  • Steven_CrawfordSteven_Crawford Posts: 3Questions: 1Answers: 0

    Awesome, That worked. Thanks Allan!

This discussion has been closed.