Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  137] [ 4]  / answers: 1 / hits: 87369  / 13 Years ago, tue, january 31, 2012, 12:00:00

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.



The question is, 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?


More From » ajax

 Answers
30

Try this:



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


If it does not solve your problem then in your php 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


[#87721] Sunday, January 29, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ammonderekm

Total Points: 247
Total Questions: 105
Total Answers: 98

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;