I write a script to make an ajax call here is my code
function ajax_post(obj) {
obj = '#'+ obj;
var formData = $(obj).serializeArray();
$.ajax({
url: '__core/info.php',
type:'get',
dataType: 'json',
data: formData,
success: function(resp){
alert(resp);
}
})
}
and here is my info.php
$last_Res = theme::get_last_themes_desk(); //$last_Res is an array
echo(json_encode($last_Res));
but when alert it shows return object object ..... what should i do if datatype is json should i convert it to another format ? $last_Res is an array
you haven't posted the json format. usually you can access resp. values like this:
if the json format is:
data['index']['subindex1'] = 'value1'
data['index']['subindex2'] = 'value2'
you can
alert(resp.index.subindex1);
In your info.php, you should set the Content-Type
header to application/json
to indicate what you are returning:
header('Content-Type: application/json');
In response to your comment (showing the response data, which is an array, containing a single object):
//resp = [{"id":"2","name":"babak_theme","sh_describ":"support css3 and ie 9 ","rate":"3","time":"2"}];
resp = resp[0];
alert('id => ' + resp.id + ', Name => ' + resp.name);//etc...
Will serve you just fine...
$last_Res
is an associative array, most likely. JS doesn't have assoc arrays, but converts these to objects/ object literals:
//php:
$foo = array('foo' => 'bar');
//in JS:
var foo = {foo: 'bar'};
alert(foo);//alerts [object Object]
console.log(foo);//shows you what properties/prototypes/methods the object has
That's all there is too it. To access the data:
for (var prop in resp)
{//for "assoc arrays"
if (resp.hasOwnProperty(prop))
{
alert(prop + ' => '+resp[prop]);
}
}
for (var i=0;i<resp.length;i++)
{//for regular arrays
alert(i + ' => ' + resp[i])'
}