How to return and process an array returned by a php file using ajax?
I've a php file which processes a query and returns an array.
How do i get these and array a开发者_开发问答nd parse them and display them using ajax.
I call the file using ajax. Its used to display the matching products while typing a text box with the price...
responseText doesn't return anything...
Your data needs to be encoded in a format that JavaScript can understand, like JSON. You want to serialize the PHP array and return that as the HTTP Response, more information on how to JSON serialize data can be found here. Once the data is serialized, you can parse it in the ResponseText as though it were a JavaScript object (i.e. you can pull data like this: ResponseText[0].some_key
)
I should note that jQuery makes this very, VERY easy. Like... this easy:
var url = '/json-data.php?id=2';
$.getJSON(url, function(data) {
$("#target").text(data.some_key);
}
精彩评论