Reputation: 1648
I'm sending a JSON object to PHP using jQuery via
$.ajax({
url: myURL,
type: 'POST',
contentType: "application/json; charset=utf-8",
data: myData,
processData: false,
dataType: 'html',
async: false,
success: function(html) {
window.console.log(html);
}
});
and trying to decode the JSON object using
$GLOBALS["HTTP_RAW_POST_DATA"];
but the contents of variable are printed as
[object Object]
with json_decode() returning NULL (of course).
Any ideas what I need to do to get the at the actual JSON data?
Thanks, Gaz.
Upvotes: 1
Views: 2697
Reputation: 27866
You are actually sending a string through POST. I recommend using JSON2 to stringify your Javascript object. Use
var myData = JSON.stringify(myObject, replacer);
Upvotes: 0
Reputation: 72
Looks like you are sending a string to the PHP. Jquery by default sends data in a normal post format. PHP can read this data just fine. I would recommend just getting the data you need out of the POST array.
If you are trying to serialize a Javascript object via JSON and then convert it back to an object in the PHP side, then you might want to go the JSON route. You will need a plugin to convert the data from a string to JSON. You might want to consider: http://code.google.com/p/jquery-json/
You would change the line:
data: myData,
To:
data: $.toJSON(myData),
Then on the PHP side you will still receive the data in the post array and you can convert it with the following command:
$params = json_decode($_POST[]);
Upvotes: 4
Reputation: 185
You've set your dataType to 'html' in your ajax call. Shouldn't it be 'json'? I think your nice json object is being condensed down into a meaningless string.
Upvotes: -1
Reputation: 14642
Looks like you do not send a JSON object to your php script, just the string 'object Object'.
Upvotes: 1
Reputation: 25263
Have you tried using $_POST?
I handle all of my JSON requests more or less like this:
$params = json_decode($_POST[]);
Upvotes: 0
Reputation: 41040
Use file_get_contents('php://input')
instead $GLOBALS["HTTP_RAW_POST_DATA"];
Upvotes: -1