This question already has an answer here:
这个问题在这里已有答案:
- How do I return the response from an asynchronous call? 31 answers
- 如何从异步调用返回响应? 31个答案
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的true或false函数?我无法弄清楚如何使这个工作。
$("#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()); // =未定义
What am I doing worng?
我在做什么?
3 个解决方案
#1
10
ajax
request are asyncronous
. 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请求是异步的。不要使用sync:true选项,这不是一个好主意。你可以做的是使用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";
}
});
#2
3
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 test() {
var promise = validateUserDetails();
promise.done(function(result) {
console.log("Bool: " + result);
});
}
#3
1
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”用于异步函数。你必须做一些成功的功能,谁使用“数据”(日志,或其他功能)
#1
10
ajax
request are asyncronous
. 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请求是异步的。不要使用sync:true选项,这不是一个好主意。你可以做的是使用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";
}
});
#2
3
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 test() {
var promise = validateUserDetails();
promise.done(function(result) {
console.log("Bool: " + result);
});
}
#3
1
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”用于异步函数。你必须做一些成功的功能,谁使用“数据”(日志,或其他功能)