PHP/Ajax/Json-来自PHP的JSON回显冻结了脚本? [英] PHP/Ajax/Json - JSON echo from PHP freezes script?
问题描述
我通常通过搜索找到答案,但这使我感到困惑,并且找不到任何相关文章:/
I normally find my answers through searches, but this ones got me stumped and I can't find any related articles :/
我只是运行对我的PHP脚本的AJAX调用,并警告返回的值(JSON编码的对象).
I am simply running an AJAX call to my PHP script and alerting the returned value (JSON encoded object).
问题是,脚本一旦击中我的"echo"语句,便会冻结.我已经在没有回声的情况下进行了测试,甚至没有使用"Hello"(两者都成功)之类的值.我还使用在网上找到的示例JSON字符串测试了输出.这失败了.
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.
我现在认为,任何构造为JSON的字符串都会导致此错误(我已经在jsonlint.com上测试了这两个JSON脚本).
I am now believing that any string structured as JSON will cause this error (I have tested both JSON scripts on jsonlint.com).
非常感谢所有帮助!
JavaScript代码:
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代码:
<?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;
?>
推荐答案
嗯,
我正在研究推理,但我相信这应该足以帮助遇到此问题的任何人:
I am looking into reasoning but I believe this should be sufficient to help anyone who runs into this problem:
我的JSON字符串中的双引号(编码对象时json_encode创建的双引号)导致'echo'语句失败.为了解决这个问题,我使用以下命令将所有未转义的双引号替换为转义的双引号:
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) )
我相信这是json_encode自身没有转义双引号的结果,并且仅在尝试从ajax请求调用的外部脚本中回显"时发生.
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.
谢谢大家:D
这篇关于PHP/Ajax/Json-来自PHP的JSON回显冻结了脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!