i have used ajax in the code which works perfectly and give me json or array which ever i want as an output. the code i have used is,

xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://map_ajax_control.php",false);
xmlhttp.send();

var test = xmlhttp.responseText;
alert(test);

This test variable gives me json/array.

I want to get the data which i received in the test variable in the JavaScript array.

newxt question i, how can i decode json data in javascript array? i have used the code as,

var output = new Array();  
output = json_decode(xmlhttp.responseText);

but this code is not giving me any output.
How can i do this two things?

link|improve this question

75% accept rate
1  
Use jQuery. Seriously. – Ates Goral Jan 31 at 5:30
feedback

3 Answers

up vote 1 down vote accepted

Try this:

var arr = xmlhttp.responseText.Split(',');

If it does not solove your problem then in yourphp code, use simple json_encode(your array); and on javascript, use myData= eval("(" + xmlHttp.responseText + ")"); .

I suggest you to follow this approach:

Encode the data you want to send by using a PHP binding for JSON at the server and decode the same using Javascript library for JSON. as:

var myObject = eval('(' + myJSONtext + ')');

or

var myObject = JSON.parse(myJSONtext, reviver);

Note: Include json2 javascript file to your solution..

Problem with storing values in Array from php to AJAX

link|improve this answer
feedback

Most browsers support JSON.parse(). Its usage is simple:


obj = JSON.parse(xmlhttp.responseText);
alert(obj.length);

For the browsers that don't you can implement it using json2.js.

link|improve this answer
i have used this also.But its not working.It is not giving me any output – Arpi Patel Jan 31 at 5:41
feedback

json is nothing but javascript object notation. You just need to parse it as suggested by Sudhir. You can also use jQuery.parseJSON for it.

And to do ajax, I strongly suggest you to use some library, preferably jQuery.

http://api.jquery.com/jQuery.ajax/

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.