$.getJSON 在某些浏览器中的问题
我有一个 php 文件,通过它输出 json 编码文本
echo '(' . json_encode( $final ) . ')';
我有一个 javascript 文件,可以获取该页面
$.getJSON(file, function(data){
var object = eval(data);
alert(object); //for testing
...
当 firefox 3.5 以外的任何浏览器访问调用 .getJSON 的页面时,它都会发出警报 null
但是!!!如果我将 php 文件输出的文本粘贴到一个新文件中并通过 .getJSON 加载它,它就可以正常工作。只有当它由 php 输出时它才不起作用。
我能看到的唯一区别是 PHP 文件的内容长度比另一个文件多 2,我不明白为什么。
感谢
UPDATE
I created a small array to test it with other data and it's working. There's something in my data that's causing the issue. Looking now...
对 array_merge 的调用是罪魁祸首。
I have a php file that outputs json encoded text via
echo '(' . json_encode( $final ) . ')';
And i have a javascript file that fetches that page
$.getJSON(file, function(data){
var object = eval(data);
alert(object); //for testing
...
When any browser other than firefox 3.5 visits the page that calls .getJSON it alerts null
BUT!!! If i take the text that is output by the php file paste it into a new file and load that via .getJSON it works fine. It's only when its output by php that it doesn't work.
The only difference i can see is that the PHP file's content length is 2 more than the other, i can't figure out why.
Thanks
UPDATE
I created a small array to test it with other data and it's working. There's something in my data that's causing the issue. Looking now...
a call to array_merge is the culprit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
data
不是一个字符串,它是一个 JSON 对象。因此 eval 不会对其起作用。请尝试以下方法:data
is not a string, it is a JSON object. Therefore eval will not work on it. Try the following instead:我已将范围缩小到对
array_merge
的调用,该调用以某种方式损坏了数据。I've narrowed it down to a call to
array_merge
that is corrupting the data somehow.