Note that there are some explanatory texts on larger screens.

plurals
  1. POparse array with JSON from xmlhttp.responseText
    text
    copied!<p>Im trying to get php array to js using ajax and encoding by JSON in PHP and decoding in JS but unable to get the desired result.</p> <p>I use var_dump just before i echo the encoded string and this is the output:</p> <pre class="lang-php prettyprint-override"><code>$boardArrayEncoded = json_encode($boardArray); var_dump($boardArrayEncoded); echo $boardArrayEncoded; </code></pre> <p>this string after JSON_encode is: </p> <pre><code>string '{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0,"2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5":0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0,"6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}' </code></pre> <p>in javascript i try parse it:</p> <pre class="lang-js prettyprint-override"><code>if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) { document.getElementById("logDiv1").innerHTML = xmlhttp.responseText; array = JSON.parse(xmlhttp.responseText); } </code></pre> <p>this is the xmlhttp.responseText: <code>{"1x1":0,"1x2":0,"1x3":0,"1x4":0,"1x5":0,"1x6":0,"2x1":0,"2x2":0,"2x3":0,"2x4":0,"2x5":0,"2x6":0,"3x1":0,"3x2":0,"3x3":0,"3x4":0,"3x5":0,"3x6":0,"4x1":0,"4x2":0,"4x3":0,"4x4":0,"4x5":0,"4x6":0,"5x1":0,"5x2":0,"5x3":0,"5x4":0,"5x5":0,"5x6":0,"6x1":0,"6x2":0,"6x3":0,"6x4":0,"6x5":0,"6x6":0}</code></p> <p>On firebug i get syntax error: </p> <pre><code>SyntaxError: JSON.parse: unexpected character [Break On This Error] array1 = JSON.parse(xmlhttp.responseText); </code></pre> <p>what im doing wrong? i need to use this array in JS, how can I encode it properly?</p> <p>Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload