POST json data via ajax send an empty array

I am trying to send data via ajax, this is my info:

var jsondata = {"address" : [ { "id": addid, "streetaddress": streetaddress, "city": city, "state": state, "zipcode": zipcode, "latitude": latitude}, ] }; var jsontosend = JSON.stringify(jsondata, null, 2); 

Ajax function:

  $.ajax({ type: "POST", url: "process.php", contentType: "application/json; charset=utf-8", dataType: "JSON", data: jsontosend, success: function(msg){ alert(msg); } }); return false; alert('Data sent'); 

}

at the end of php when i print_r ($ _ POST) it just says

  array(0) { } 

I warn (jsontosend) and it shows me everything perfectly, also in firebug using post mothod, and shows all the parameters sent in a perfectly clean way.

The only way to transfer data is to use the GET method.

Any advice is appreciated!

EDIT: adding POST data from firebug. this is what is being warned from the alert function:

  {"address":[{"id":1473294,"streetaddress":"3784 Howard Ave","city":"Washington DC","state":"DC","zipcode":20895,"latitude":39.027820587}]} 

this is what firebug shows as passed when using the POST method:

  myData=%7B%0A++++%22address%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22id%22%3A+76076%2C%0A++++++++++++%22streetaddress%22%3A+%223784+Howard+Ave%22%2C%0A++++++++++++%22city%22%3A+%22Washington+DC%22%2C%0A++++++++++++%22state%22%3A+%22DC%22%2C%0A++++++++++++%22zipcode%22%3A+20895%2C%0A++++++++++++%22latitude%22%3A+39.027820587%0A++++++++%7D%0A++++%5D%0A%7D 

and this is the answer for var_dump $ _POST:

  array(0) { 

}

this is var_dump $ _POST ['myData']

  NULL 
+7
source share
7 answers

I am skeptical about how you use the contentType property. Try taking out contentType. The default content type is application / x-www-form-urlencoded ( http://api.jquery.com/jQuery.ajax/ ).

Also, for your data property, use something like {mydata: jsontosend}.

 $.ajax({ type: "POST", url: "process.php", //contentType: "application/json; charset=utf-8", dataType: "JSON", data: {mydata: jsontosend}, success: function(msg){ alert(msg); } }); 
+7
source

Using

data:{myData: jsontosend}

It should send myData as a parameter to your request.

+2
source

PHP does not understand application / json requests (default). See This Question: How to send JSON to PHP with curl

+2
source

I found that the comment provided by dkulkarni was the best solution here. Must be read directly from the input stream to receive POST data of any complexity. Changing the value of $.ajax({contentType: ''}) did not help me.

To fix this problem, I configured the controller method to track the JSON that I sent to my server. Changing the ContentType header didn't make any difference. But after reading dkulkarni's comment, I changed the server code from this:

  return $_POST; 

:

  $data = file_get_contents('php://input'); return json_decode($data); 

It worked great.

+2
source

As dkulkarni noted in his post (January 24 at 7:53), the only way to get JSON sent using the Content-Type / json application seems to be to get it directly from the input stream.

I did a lot of testing with different approaches, but whenever I set the Content-Type to JSON, $ _POST in PHP is empty.

My problem: I need to set the Content-Type to JSON because the server (where I do not have access) expects it, otherwise it returns an unsupported data type. I believe that they are extracted directly from the input stream.

Why should I reproduce this in my own PHP file? Because I need to make a proxy to test problems, but this is not the point of this thread.

+1
source

Try removing 'dataType: "JSON" from the ajax request and check.

0
source

Taking contentType is not a good idea. You want to send this data in JSON format. The key is to do what was mentioned above, instead of trying to access the data on the receiving side (target php file) using $ data = $ _POST, it is important to use $ data = file_get_contents ('php: // input') ;

Here is an example of the whole round trip for the script with php and knockout.js:

JS (on request page):

 function() { var payload = ko.toJSON({ YOUR KNOCKOUT OBJECT }); jQuery.ajax({ url: 'target.php', type: "POST", data: payload, datatype: "json", processData: false, contentType: "application/json; charset=utf-8", success: function (result) { alert(result); } }); }; 

And then in target.php (the page receiving the request):

 $data = file_get_contents('php://input'); $payload_jsonDecoded = json_decode($data 
0
source

All Articles