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.
-
What is the contents of myData? Can you output (using firebug, e.g.) that to make sure it has the correct contents (and post the results)?jonstjohn– jonstjohn2009年11月19日 14:58:58 +00:00Commented Nov 19, 2009 at 14:58
-
I've printed it out using window.console.log and it's correct.Gaz– Gaz2009年11月19日 15:33:22 +00:00Commented Nov 19, 2009 at 15:33
-
Object ar: Array content: "Some more test data" link_title: "" title: "" en: Array content: "My test data" link_title: "" title: ""Gaz– Gaz2009年11月19日 15:34:45 +00:00Commented Nov 19, 2009 at 15:34
6 Answers 6
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[]);
1 Comment
Looks like you do not send a JSON object to your php script, just the string 'object Object'.
1 Comment
Have you tried using $_POST?
I handle all of my JSON requests more or less like this:
$params = json_decode($_POST[]);
1 Comment
You are actually sending a string through POST. I recommend using JSON2 to stringify your Javascript object. Use
var myData = JSON.stringify(myObject, replacer);
Comments
Use file_get_contents('php://input')
instead $GLOBALS["HTTP_RAW_POST_DATA"];
1 Comment
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.