1

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.

asked Nov 19, 2009 at 14:47
3
  • 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)? Commented Nov 19, 2009 at 14:58
  • I've printed it out using window.console.log and it's correct. Commented 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: "" Commented Nov 19, 2009 at 15:34

6 Answers 6

4

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[]);
answered Nov 19, 2009 at 15:56
Sign up to request clarification or add additional context in comments.

1 Comment

OK, it works now. An additional problem was using non-numeric keys in a javascript array. Used $.toJSON and changed to keys to numeric and it works. By the way, I didn't use $_POST but used contentType: "application/json; charset=utf-8", and processData: false, then decoded the JSON from $GLOBALS["HTTP_RAW_POST_DATA"]. Cheers, Gaz.
1

Looks like you do not send a JSON object to your php script, just the string 'object Object'.

answered Nov 19, 2009 at 14:54

1 Comment

Yeah, I can see that! Do you know what I need to change in the jQuery AJAX call to do otherwise? Thanks, Gaz.
0

Have you tried using $_POST?

I handle all of my JSON requests more or less like this:

$params = json_decode($_POST[]);
answered Nov 19, 2009 at 14:52

1 Comment

The result is the same, an empty array.
0

You are actually sending a string through POST. I recommend using JSON2 to stringify your Javascript object. Use

var myData = JSON.stringify(myObject, replacer);

answered Nov 19, 2009 at 16:06

Comments

-1

Use file_get_contents('php://input') instead $GLOBALS["HTTP_RAW_POST_DATA"];

answered Nov 19, 2009 at 14:49

1 Comment

The two are equivalent anyway.
-1

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.

answered Nov 19, 2009 at 14:55

1 Comment

dataType -> The type of data that you're expecting back from the server.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.