首先我已经阅读过这个主题jQuery AJAX函数返回true或false只返回false而它的一切都很好,在回调函数中返回jQuery的ajax数据参数是什么?,如何返回数据响应ajax的真或假功能?我无法弄清楚如何使这个工作.
$("#btn_go").on('click', function(){
if(validateUserDetails() == false){
return;
}
});
Run Code Online (Sandbox Code Playgroud)
所以,该功能validateUserDetails有以下几点:
function validateUserDetails(){
var bool = false;
$.ajax({
url: 'response.php?type=validateUserDetails',
type: 'POST',
dataType: 'json',
data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(),
"city": $("#checkout_city").val()},
success: function(data){
console.log(data); // this is currently returning FALSE
// Which is totally correct!
if(data == true){ bool = true; }
return trueOrFalse(bool);
}
});
}
function trueOrFalse(bool){
return bool;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为如果我输出函数我得到"未定义",这意味着该函数没有重新调整正确的值. console.log(validateUserDetails()); // = undefined
我在做什么?
ste*_*teo 13
ajax要求是asynchronous.不要使用该sync: true选项,这不是一个好主意.你可以做的是使用promise的ajax回报,所以:
function validateUserDetails(){
return $.ajax({
url: 'response.php?type=validateUserDetails',
type: 'POST',
async: false,
dataType: 'json',
data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(),
"city": $("#checkout_city").val()},
success: function(data){
console.log(data); // this is currently returning FALSE
}
});
}
$("#btn_go").on('click', function(){
validateUserDetails().done(function(data){
if(data == "someValue")
return "whatever you want";
});
});
Run Code Online (Sandbox Code Playgroud)
}
function validateUserDetails(){
return $.ajax({
url: 'response.php?type=validateUserDetails',
type: 'POST',
async: false,
dataType: 'json',
data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(),
"city": $("#checkout_city").val()},
success: function(data){
console.log(data); // this is currently returning FALSE
}
});
}
$("#btn_go").on('click', function(){
validateUserDetails().done(function(data){
if(data == "someValue")
return "whatever you want";
});
});
Run Code Online (Sandbox Code Playgroud)
正如没有人回答的那样,我将:
首先,您可以尝试同步请求
function validateUserDetails() {
var bool = false;
$.ajax({
url: 'response.php?type=validateUserDetails',
type: 'POST',
async: false,
dataType: 'json',
data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(), "city": $("#checkout_city").val()},
success: function(data) {
console.log(data); // this is currently returning FALSE
// Which is totally correct!
if (data == true) {
bool = true;
}
}
});
return trueOrFalse(bool);
}
Run Code Online (Sandbox Code Playgroud)
如果不可接受,则可以使用$ .Deferred()
function validateUserDetails() {
var deferred = $.Deferred();
var bool = false;
$.ajax({
url: 'response.php?type=validateUserDetails',
type: 'POST',
dataType: 'json',
data: {name: $("#checkout_name").val(), email: $("#checkout_email").val(), "country": $("#checkout_country").val(), "city": $("#checkout_city").val()},
success: function(data) {
console.log(data); // this is currently returning FALSE
// Which is totally correct!
if (data == true) {
bool = true;
}
}
complete: function () {
deferred.resolve(trueOrFalse(bool));
}
});
return deferred.promise();
}
function trueOrFalse(bool){
return bool;
}
function test() {
var promise = validateUserDetails();
promise.done(function(result) {
console.log("Bool: " + result);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49014 次 |
| 最近记录: |