简体   繁体   中英

Issue with encoded array from php

I'm having an issue returning a value from PHP to Javascript. I have encoded the PHP array like so :

echo json_encode($myArray);

And on the Javascript side i do this within the $.ajax method:

success:function (data) {
  alert(data);
}  

This works and it alerts the returned array, however when i try to then set my Javascript array to the value of data :

success:function (data) {
  myArray = data;
}  

This completely breaks my looping operation and so instead of printing out for example:

This is a test

It will print:

t,h,i,s,i,s,a,t,e,s,t

and the length of the array rather than being 4, for 4 words it is 16+ including the square brackets etc. How can i reuse the json encoded array once it has been recieved by javascript and maintain its structure?

Related: Parse JSON in JavaScript?

What you are looking for:

myArray = JSON.parse(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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