简体   繁体   English

为什么此JSON.parse代码不起作用?

[英]Why does this JSON.parse code not work?

I am trying to pass json encoded values from a php script to a, GnuBookTest.js, javascript file that initiates a Bookreader object and use the values i have passed in via the variable i named "result". 我试图将json编码的值从php脚本传递到一个GnuBookTest.js javascript文件,该文件启动Bookreader对象并使用我通过名为“结果”的变量传递的值。

The php script is sending the values like: php脚本发送的值如下:

<div id="bookreader">
 <div id="BookReader" style="left:10px; right:10px; top:30px; bottom:30px;">x</div>
 <script type="text/javascript">var result = {"istack":"zi94sm65\/BUCY\/BUCY200707170530PM","leafCount":"14","wArr":"[893,893,893,893,893,893,893,893,893,893,893,893,893,893]","hArr":"[1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155,1155]","leafArr":"[0,1,2,3,4,5,6,7,8,9,10,11,12,13]","sd":"[\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\",\"RIGHT\",\"LEFT\"]"}</script>
 <script type="text/javascript" src="http://localhost:8080/application/js/GnuBookTest.js"></script>
 </div>
</div>

and in the GnuBookTest.js file i am trying to use the values like: 在GnuBookTest.js文件中,我尝试使用以下值:

br = new BookReader();

// Return the width of a given page.
br.getPageWidth = function(index) {
     return this.pageW[index];
}

// Return the height of a given page.
br.getPageHeight = function(index) {
    return this.pageH[index];
}

br.pageW = JSON.parse(result.wArr);

br.pageH = JSON.parse(result.hArr);

br.leafMap = JSON.parse(result.leafArr);

//istack is an url fragment for location of image files
var istack = result.istack;
.
.
.

Using JSON.parse as i have written it above loads the Bookreader and uses my values correctly in a few web-browsers: Firefox, IE8, and desktop-Safari; 使用我上面编写的JSON.parse可以加载Bookreader并在一些网络浏览器中正确使用我的值:Firefox,IE8和桌面Safari; but does not work at all in mac-Chrome, mobile-Safari, plus older versions of IE. 但在Mac-Chrome,移动Safari和旧版IE中根本无法使用。 Mobile safari keeps giving me a reference error msg: can't find variable: JSON. 移动浏览器不断给我一个参考错误msg:找不到变量:JSON。 The other browsers just do not load the Bookreader and show the "x" instead, like they did not get the values from the php script. 其他浏览器只是不加载Bookreader而是显示“ x”,就像它们没有从php脚本中获取值一样。

Where is the problem? 问题出在哪儿?

Older browsers don't have native JSON support. 较旧的浏览器没有本机JSON支持。 You'll likely have to include it manually. 您可能必须手动添加它

Older browsers do support JSON, they just don't support JSON.parse . 较旧的浏览器确实支持JSON,但不支持JSON.parse To load JSON data in older browsers you can simply use 要在旧版浏览器中加载JSON数据,您只需使用

var obj = eval('('+jsonStr + ')');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM