Why does this JSON.parse code not work?

Posted by SuZi on Stack Overflow See other posts from Stack Overflow or by SuZi
Published on 2010-03-28T18:58:03Z Indexed on 2010/03/28 19:03 UTC
Read the original article Hit count: 321

Filed under:
|
|
|
|

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".

The php script is sending the values like:

<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:

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; but does not work at all in mac-Chrome, mobile-Safari, plus older versions of IE. Mobile safari keeps giving me a reference error msg: can't find variable: 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.

Where is the problem?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about parsing