[英]Wordpress ajax with async false
Inside WordPress, I want to use ajax return value outside the function 在WordPress内部,我想在函数外部使用Ajax返回值
Example 例
function get_login_member($) {
$.post(ajax_object.ajax_url, {action: 'getloginmember'}, function (data) {
data = JSON.parse(data);
if (data['id'] > 0) {
return data;
} else {
return 0;
}
});
}
And this function calling from 而这个函数从
var row = get_login_member($);
The row
variable is undefined
because it executes before ajax request success 该
row
变量undefined
因为它在ajax请求成功之前执行
With Jquery ajax this can be done by made async:false
使用jQuery ajax,可以通过使
async:false
来完成
Is there any way to do this inside WordPress 有没有办法在WordPress内部执行此操作
forget sycnhronous ajax - easiest solution is to use a callback 忘记sycnhronous ajax-最简单的解决方案是使用回调
function get_login_member($, cb) {
$.post(ajax_object.ajax_url, {action: 'getloginmember'}, function (data) {
data = JSON.parse(data);
if (data['id'] > 0) {
cb(data);
} else {
cb(0);
}
});
}
Then 然后
get_login_member($, function(row) {
// put your code here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.