如何在下面的jQuery的例子正确返回对象返回对象: -通过jQuery JSON请求
function get_stockists() {
$.getJSON("/stockists/ajax_get_all", function(data) {
//console.log(data);
});
}
var stockists = get_stockists();
console.log(stockists);
如何在下面的jQuery的例子正确返回对象返回对象: -通过jQuery JSON请求
function get_stockists() {
$.getJSON("/stockists/ajax_get_all", function(data) {
//console.log(data);
});
}
var stockists = get_stockists();
console.log(stockists);
最好的方法是不返回,而是使用一个回调函数:
function getStockists(callback) {
$.getJSON("/stockists/ajax_get_all", callback);
}
getStockists(function(stockists) {
console.log(stockists);
});
Ajax是异步的(异步JavaScript和XML),因此您需要提供回调方法。 –