I have used jquery validation to validate email. But the variable whose value is changed in success function is not accessible outside it. It can be done with async=false, but that destroys the purpose of Ajax.
我使用jquery验证来验证电子邮件。但是在其成功函数中改变其值的变量在其外部是不可访问的。它可以用async = false来完成,但这会破坏Ajax的目的。
Below is the code:
以下是代码:
$.validator.addMethod('verifyemail',function(value,element){
$.ajax({
type: "POST",
url : $.app.urls('base_url')+'account/check_email',
data: {'email' : value},
success: function(msg){
//If email exists, set response to true
if( msg.status == 'false' )
respond = false;
else
respond = true;
}
})
console.log(respond);
return respond;
}, "Email is Already Taken");
4 个解决方案
#1
4
You can't set the value in an asynchronous function like that. The way in which this works is that respond
would be set to a value. Then your asynchronous ajax call would fire off, as soon as it fires off the rest of the code below your ajax call would execute. Once the ajax call returns, the code in the success callback will be executed. Most likely, by this time your validator function would have finished executing.
您不能在这样的异步函数中设置该值。这种方法的工作方式是将响应设置为一个值。然后你的异步ajax调用将触发,一旦它触发你的ajax调用下面的其余代码就会执行。一旦ajax调用返回,将执行成功回调中的代码。最有可能的是,此时验证器函数将完成执行。
Since you're using this ajax call in a validator, you would want to run this in series since the validation depends on the result of your ajax call. You could accomplish this by setting async: false
.
由于您在验证器中使用此ajax调用,因此您需要将其串联运行,因为验证取决于您的ajax调用的结果。您可以通过设置async:false来完成此操作。
Here is a useful resource on asynchronous callbacks and how they work:
这是异步回调及其工作原理的有用资源:
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
And another covering jQuery's AJAX function async vs sync:
另一个覆盖jQuery的AJAX函数async vs sync:
http://net.tutsplus.com/tutorials/javascript-ajax/event-based-programming-what-async-has-over-sync/
http://net.tutsplus.com/tutorials/javascript-ajax/event-based-programming-what-async-has-over-sync/
#2
1
Ajax call is asynchronous, meaning when you call console.log(respond); The variable respond is still not available. If you need to access it outside the success function, you can trigger a custom event and get respond in the event handler.
Ajax调用是异步的,这意味着当你调用console.log(response)时;变量响应仍然不可用。如果需要在success函数之外访问它,可以触发自定义事件并在事件处理程序中获得响应。
And of course, you have to put respond in outer scope.
当然,你必须在外部范围内做出回应。
#3
0
to access the respond variable you need to either define it in upper scope
要访问响应变量,您需要在上限范围内定义它
$.validator.addMethod('verifyemail',function(value,element){
var respond = true; // scope of variable to current function
$.ajax({
type: "POST",
url : $.app.urls('base_url')+'account/check_email',
async: false,
data: {'email' : value},
success: function(msg){
//If email exists, set response to true
if( msg.status == 'false' )
respond = false;
else
respond = true;
}
})
console.log(respond);
return respond;
}, "Email is Already Taken");
#4
0
respond
is already a global variable (lack of var
keyword), but the asynchronous nature of AJAX means that it won't necessarily be available when the following lines run, so you need to sequence it in one of a few ways
response已经是一个全局变量(缺少var关键字),但AJAX的异步性质意味着当以下行运行时它不一定可用,所以你需要以几种方式之一对它进行排序
- Make it synchronous (but the question rules that out)
- 使它同步(但问题规则出来)
- put the responding code inside of the same anonymous function
- 将响应代码放在同一个匿名函数中
- You could also make a
while
loop that spins until respond is set, but that would probably be a poor solution. - 您还可以创建一个while循环,直到响应设置为止,但这可能是一个糟糕的解决方案。
Hope that helped
希望有所帮助
#1
4
You can't set the value in an asynchronous function like that. The way in which this works is that respond
would be set to a value. Then your asynchronous ajax call would fire off, as soon as it fires off the rest of the code below your ajax call would execute. Once the ajax call returns, the code in the success callback will be executed. Most likely, by this time your validator function would have finished executing.
您不能在这样的异步函数中设置该值。这种方法的工作方式是将响应设置为一个值。然后你的异步ajax调用将触发,一旦它触发你的ajax调用下面的其余代码就会执行。一旦ajax调用返回,将执行成功回调中的代码。最有可能的是,此时验证器函数将完成执行。
Since you're using this ajax call in a validator, you would want to run this in series since the validation depends on the result of your ajax call. You could accomplish this by setting async: false
.
由于您在验证器中使用此ajax调用,因此您需要将其串联运行,因为验证取决于您的ajax调用的结果。您可以通过设置async:false来完成此操作。
Here is a useful resource on asynchronous callbacks and how they work:
这是异步回调及其工作原理的有用资源:
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/
And another covering jQuery's AJAX function async vs sync:
另一个覆盖jQuery的AJAX函数async vs sync:
http://net.tutsplus.com/tutorials/javascript-ajax/event-based-programming-what-async-has-over-sync/
http://net.tutsplus.com/tutorials/javascript-ajax/event-based-programming-what-async-has-over-sync/
#2
1
Ajax call is asynchronous, meaning when you call console.log(respond); The variable respond is still not available. If you need to access it outside the success function, you can trigger a custom event and get respond in the event handler.
Ajax调用是异步的,这意味着当你调用console.log(response)时;变量响应仍然不可用。如果需要在success函数之外访问它,可以触发自定义事件并在事件处理程序中获得响应。
And of course, you have to put respond in outer scope.
当然,你必须在外部范围内做出回应。
#3
0
to access the respond variable you need to either define it in upper scope
要访问响应变量,您需要在上限范围内定义它
$.validator.addMethod('verifyemail',function(value,element){
var respond = true; // scope of variable to current function
$.ajax({
type: "POST",
url : $.app.urls('base_url')+'account/check_email',
async: false,
data: {'email' : value},
success: function(msg){
//If email exists, set response to true
if( msg.status == 'false' )
respond = false;
else
respond = true;
}
})
console.log(respond);
return respond;
}, "Email is Already Taken");
#4
0
respond
is already a global variable (lack of var
keyword), but the asynchronous nature of AJAX means that it won't necessarily be available when the following lines run, so you need to sequence it in one of a few ways
response已经是一个全局变量(缺少var关键字),但AJAX的异步性质意味着当以下行运行时它不一定可用,所以你需要以几种方式之一对它进行排序
- Make it synchronous (but the question rules that out)
- 使它同步(但问题规则出来)
- put the responding code inside of the same anonymous function
- 将响应代码放在同一个匿名函数中
- You could also make a
while
loop that spins until respond is set, but that would probably be a poor solution. - 您还可以创建一个while循环,直到响应设置为止,但这可能是一个糟糕的解决方案。
Hope that helped
希望有所帮助