I've been going bananas trying to get some data from Javascript on one page to post to a php file asynchronously. I'm not even attempting to do anything with the data, just a var_dump to spit back out from the ajax call. NULL over and over again.

I've checked the JSON with JSONLint and it validates just fine. I'm getting my JSON from JSON.stringify - Firebug tells me I'm getting the following:

{"items":[["sa1074","1060"],["sa1075","1061"]]}

I've tried php://input, as well as json_decode_nice from the PHP manual comments about the function, and I've tried using utf8_encode - is there something wrong with my JSON?

EDIT: Derpity dee probably should have planned this post a bit more haha. Here's my PHP (using a suggestion from PHP manual comments)

function json_decode_nice($json, $assoc = FALSE){
$json = str_replace(array("\n","\r"),"",$json);
$json = preg_replace('/([{,])(\s*)([^"]+?)\s*:/','$1"$3":',$json);
return json_decode($json,$assoc);
}
if (isset($_POST['build'])){
    $kit = file_get_contents('php://input');
    var_dump(json_decode_nice($kit));
}

And the JS used to create it:

        var jsonKit = JSON.stringify(kit);
    $.post("kits.php?", {"build" : jsonKit},
        function(data) {
            $("#kitItems").html(data);
        });

Also: Our host is on PHP 5.2 - I found this out when I uploaded a perfectly good redbean class only to have it explode. Had to re-implement with legacy redbean. Our host is busted.

SOLVED: Thanks to all who commented. Didn't think to check $_POST to see what was coming in. Quotes were being escaped with slashes in the $_POST and json_decode was choking on it. Adding this line before decoding solved the problem. Also forgot to set true to return an associative array. Thanks!

$kit = str_replace('\\', '', $kit);
share|improve this question

1  
Please show more code, especially how you make the call. If var_dump($_POST) results in NULL, your problem is earlier up the chain – Pekka Dec 9 '10 at 21:42
Please post your JS and your PHP. :) – Alex Dec 9 '10 at 21:42
Don't forget to make friends with json_last_error(), although this sounds like the problem lies elsewhere. – mario Dec 9 '10 at 21:44
Thanks for the comments! I hadn't thought to dump ($_POST) - I'm just so used to posting stuff and not worrying about it. I guess you have to go back to basics. Found out that stuff was being escaped with slashes when I dumped ($_POST). Thanks! – James Dec 10 '10 at 13:49
1  
And magic quotes screw up yet another PHP program... php.net/manual/en/security.magicquotes.disabling.php – Ignacio Vazquez-Abrams Dec 10 '10 at 13:54
show 2 more comments
feedback

1 Answer

up vote 2 down vote accepted

Without seeing code - I'm just guessing here ... are you using a true assoc variable in your json_decode()?

<?php json_decode($jsonString, true); ?>

That had me stumped on a decode for quite some time my first time trying to decode something,

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.