how to get a JavaScript array as result from jQuery.ajax()?
I'm using something like this,
jQuery(function($) {
function updateData()
{
var html = $.ajax({
url: 'ajaxMap.php?id='+lastId,
success: function(data) {
alert(data);
}
});
}
setInterval(function(){
updateData()
}, 2000);
});
I expect to get a JavaScript array after querying the PHP file. As shown in the code, 'data' is returned after querying the url (The PHP fi开发者_如何转开发le simply outputs a JavaScript array). When I use alert(data), it shows me the same array. But I cannot use this as an array. For example, I can't use array functions like .length, etc on this. JavaScript treats this as a simple string. What am I doing wrong here? Let me know if I need to provide more information.
Use this JS (not tested)
$(document).load(function(){
setInterval(function() {
$.getJSON('ajaxMap.php?id='+lastId,function(data) {
alert(data);
});
});
});
And json_encode($array)
on server side ('application/json' MIME header would be great but it is not needed)
You need to parse Json string like this or this
精彩评论