[英]Ajax success function not running
我有以下我提出的Ajax请求。 当前,如果json响应为true
则运行成功回调。 但是,如果json响应为false
则即使Ajax请求本身已成功发出,也不会运行成功回调。
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'check_username',
user: user_email
},
success: function(json) {
if (json.user_exists == true) {
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'auto_login',
user: user_email
},
success: function(json) {
$('#aux_lightbox_overlay').fadeOut();
}
});
}
if (json.user_exists == false) {
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'auto_register',
user: user_email
},
success: function(json) {
$('#aux_lightbox_overlay').fadeOut();
}
});
}
}
});
所以我的问题是,即使响应为false
,如何使回调运行?
谢谢
也许您需要使用complete
success
和error
来代替。
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'check_username',
user: user_email
},
complete: function(json) {
if (json.user_exists == true) {
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'auto_login',
user: user_email
},
success: function(json) {
$('#aux_lightbox_overlay').fadeOut();
}
});
}
if (json.user_exists == false) {
$.ajax({
url: "http://testdomain.com/wp-admin/admin-ajax.php",
type: "POST",
dataType: "json",
cache: false,
data: {
action: 'auto_register',
user: user_email
},
success: function(json) {
$('#aux_lightbox_overlay').fadeOut();
}
});
}
}
});
您确定当用户不存在时, json.user_exists
确实等于false
吗? 如果我对您的理解正确,则不会触发if (json.user_exists == false){...}
语句,因此那里有些奇怪。
要获得更强大的代码,您可以更改
if (json.user_exists == true) {
// ...
}
if (json.user_exists == false){
// ...
}
成
if (json.user_exists == true) {
// ...
}
else {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.