1

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);
7
  • 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 Commented Dec 9, 2010 at 21:42
  • Please post your JS and your PHP. :) Commented Dec 9, 2010 at 21:42
  • Don't forget to make friends with json_last_error(), although this sounds like the problem lies elsewhere. Commented Dec 9, 2010 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! Commented Dec 10, 2010 at 13:49
  • 1
    And magic quotes screw up yet another PHP program... php.net/manual/en/security.magicquotes.disabling.php Commented Dec 10, 2010 at 13:54

1 Answer 1

2

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,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.