jQuery ajax 返回 true/false - 我已经实现了回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23078650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajax return true/false - I have implemented a callback
提问by Linesofcode
First of all I have read this topics jQuery AJAX function return true or false returning only false while its all good, What to return for jQuery's ajax data param in callback function?, How return true or false function of data response ajax?and I can't figure out how to put this working.
首先,我已经阅读了这个主题jQuery AJAX 函数返回 true 或 false 只返回 false 而它一切都很好,在回调函数中为 jQuery 的 ajax 数据参数返回什么?,如何返回真假数据响应ajax函数?我不知道如何让这个工作。
$("#btn_go").on('click', function(){
if(validateUserDetails() == false){
return;
}
});
So, the function validateUserDetails
has the following:
因此,该函数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;
}
But this is not working because if I output the function I get "undefined", which means that the function is not retuning the correct value. console.log(validateUserDetails()); // = undefined
但这不起作用,因为如果我输出函数,我会得到“未定义”,这意味着函数没有重新调整正确的值。 console.log(validateUserDetails()); // = undefined
What am I doing worng?
我在做什么?
回答by steo
ajax
request is asynchronous
. Don't use the sync: true
option, it's not really a good idea.
What you can do is to use the promise
that the ajax
returns, so:
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";
});
});
回答by Regent
As noone has answered yet, I will:
由于还没有人回答,我将:
First of all, you can try sync request
首先,您可以尝试同步请求
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);
}
If it is not acceptable, you can use $.Deferred()
如果不能接受,可以使用$.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);
});
}
回答by Couturier Boris
Your function validateUserDetails return no value, she just execute an asynchronous function. The asynchronous function return true or false, but nothing for catching it.
你的函数 validateUserDetails 没有返回值,她只是执行一个异步函数。异步函数返回 true 或 false,但没有捕获它。
It's like this :
就像这样 :
function validateUserDetails(){
// asynchronous function
$.ajax({...});
return undefined;
}
If you try console.log(trueOrFalse(bool));
you will see "true" or "false", but you can't use "return" into asynchronous function. You must do something into success function, who use "data" (log, or another function)
如果你尝试console.log(trueOrFalse(bool));
你会看到“true”或“false”,但你不能使用“return”进入异步函数。您必须对使用“数据”(日志或其他功能)的成功功能做一些事情