0

I normally find my answers through searches, but this ones got me stumped and I can't find any related articles :/

I am simply running an AJAX call to my PHP script and alerting the returned value (JSON encoded object).

The problem is, the script freezes as soon as it hits my 'echo' statement. I have tested without the echo, and even with values such as "Hello" (both which were successful). I also tested an output with an example JSON string that I found online. This failed.

I am now believing that any string structured as JSON will cause this error (I have tested both JSON scripts on jsonlint.com).

All help is greatly appreciated!!!

Javascript Code:

function scan()
{
var script          = "../resources/ajax/fincenmanager/load_reports.php";
var params          = "";
var return_function = "load_wire";

document.getElementById("loading_screen").className = "show";

ajax(script, params, return_function);
}
function load_wire(text)
{
document.getElementById("loading_screen").className = "hidden";
alert(text);
}

PHP Code:

<?php
     require_once("../../config.php");
     require_once("../../library/FincenManager/fincenmanagerclass.php");

     header("Content-Type: application/json");

     $manager = new FincenManager("../../inputs/FincenManager/");

     $json = json_encode($manager);

     // Script Breaks After This Line.. 100% Sure :/
     echo $json;
 ?>
trevorkavanaugh
  • 340
  • 1
  • 8
  • What's the output from `var_dump($manager)`? – WWW Aug 09 '12 at 19:21
  • If I change the echo statement to var_dump($manager) the script still freezes and output is not returned, so I am unsure :/ I am assuming that there must be some character or something that it does not like, but TBH I am lost :P – trevorkavanaugh Aug 09 '12 at 19:25
  • Do it right after you set `$manager` (before you try to `json_encode()` it). Comment out the `echo` line and the `json_encode` line. It might just be that the object you're creating can't be JSON encoded for some reason. – WWW Aug 09 '12 at 19:26
  • Just attempted that, still freezing up :/ The wierd part is, if I remove the echo statement it will run fine (it will JSON encode the object and return a blank alert statement). That is what made me believe that it is the echo statement failing. – trevorkavanaugh Aug 09 '12 at 19:29
  • 1
    Very interesting... just solved this. Apparently having double quotes in my string caused the echo to fail. I used str_replace("\"", "'", $json) to change double quotes to singles and the string returns perfectly... Anyone know why this is? (this only happens in ajax calls) – trevorkavanaugh Aug 09 '12 at 19:32
  • Alrighty I guess I figured it out.. the double quotes are "unescaped" for lack of a better word when passed back to the calling script. So they must be double escaped... I.E. str_replace("\"", "\\\"", $json) – trevorkavanaugh Aug 09 '12 at 19:36
  • You should post your answer as an answer and accept it. Just in case someone doesn't know to look through the comments. – Matt Aug 09 '12 at 19:41
  • I would like too, but cannot for about another 7 hours. I have under 10 reputation points and therefore cannot answer my own questions for at least 8 hours after I asked it :/ – trevorkavanaugh Aug 09 '12 at 20:56

2 Answers2

0

You need to ecape a lot more then just the " character (as you discovered). In this case, the problem is that the " ends the string and JSON is not expecting a character after the " but a ,, ] or an } there.

The answer of this question will help you further with the escaping of the characters: click

And for testing JSON: JSONlint

Community
  • 1
  • 1
Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
  • Thanks Steven, I looked into the link but I think the problem there was a little different. I could not even get the string to return to the calling ajax function, it froze in the PHP script and never returned. I am assuming this is because json_encode creates the string without escaped double-quotes and when attempting to echo that back it crashes. Still unsure though.. – trevorkavanaugh Aug 09 '12 at 20:41
0

Well,

I am looking into reasoning but I believe this should be sufficient to help anyone who runs into this problem:

The double quotes in my JSON string (the double quotes created by json_encode when encoding the object) were causing the 'echo' statement to fail. To resolve this I replaced all unescaped double quotes with escaped double quotes using the following:

str_replace( " \" " , " \\\" " , json_encode($object) )

I believe this to be the result of json_encode not escaping the double quotes on its own, and only happens when trying to 'echo' from an external script called from an ajax request.

Thanks Everyone :D

trevorkavanaugh
  • 340
  • 1
  • 8