php - Javascript eval() doesn't work as expected -


i'm fetching data php function in json format.

 var xhreq = new xmlhttprequest();  xhreq.open("get", "http://myserver/getjson", false);  xhreq.send(null);  var serverresponse = xhreq;  var jsondata=eval("("+serverresponse.responsetext+")") //retrieve result javascript object   images="";   for(var i=0; i<jsondata.length;i++)  {   images+="  ['"+jsondata[i].title+"','"+imagesroot+"121a.png"+"']";        if(i<jsondata.length-1)         images+=",";  } 

im getting data in following format.

['title 1','http://site.com/images/121a.png'], ['title 2','http://site.com/images/121a.png'], ['title 3','http://site.com/images/121a.png'], ['title 4','http://site.com/images/121a.png'] 

finally when im assigning data array using eval()

var tinymceimagelist = new array(eval(images)); 

it show last element of array ['title 4','http://site.com/images/121a.png'].

i want every element of array assigned.

a simple solution be

var tinymceimagelist = eval('['+serverresponse.responsetext+']'); 

or

var tinymceimagelist = json.parse(     '['+serverresponse.responsetext.replace(/'/g, '"')+']' ); 

but better solution generate json adding missing [ , ] , using proper quotes, allow use json.parse directly. note php comes in standard tools generate json.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -