Reputation: 341
how to return an array in php to ajax call,
ajax call :
$.post('get.php',function(data){
alert(data)
});
get.php
$arr_variable = array('033','23454')
echo $arr_variable;
in the alert(data), it is displaying as Array (i.e only text), when i display data[0], 1st letter of Array i.e A is displaying.
Any suggestions ? where i have done wrong
Upvotes: 5
Views: 36393
Reputation: 1018
instead of echo $arr_variable;
use echo json_encode($arr_variable);
and then in jQuery
you can access it like an object.
Once it is an object, you can access it as data[0] and so forth.
$.post('get.php',function(data){
$.each(data, function(d, v){
alert(v);
});
});
Upvotes: 1
Reputation: 28763
Use to encode the array like
$data['result'] = $arr_variable;
echo json_encode($data);
exit;
And in the success function try to get it like parseJSON
like
$.post('get.php',function(data){
var res = $.parseJSON(data);
alert(res.result)
});
Upvotes: 13