Javascript 正在尝试解决jquery异步问题
我对javascript非常陌生,我正在尝试使用mailgun的电子邮件验证功能,并且我正在尝试使验证工作正常(通过确保在提交表单之前电子邮件数据是完整的) 但是我发现函数validation_success总是异步调用的,结果如下:Javascript 正在尝试解决jquery异步问题,javascript,jquery,asynchronous,mailgun,Javascript,Jquery,Asynchronous,Mailgun,我对javascript非常陌生,我正在尝试使用mailgun的电子邮件验证功能,并且我正在尝试使验证工作正常(通过确保在提交表单之前电子邮件数据是完整的) 但是我发现函数validation_success总是异步调用的,结果如下: checks start! Feedback:390 checks complete! ErrorEmail=2 Feedback:347 execution begin Feedback:404 validation begin! ErrorEmail=2 F
checks start!
Feedback:390 checks complete! ErrorEmail=2
Feedback:347 execution begin
Feedback:404 validation begin! ErrorEmail=2
Feedback:419 validation complete! ErrorEmail=2
var ErrorEmail = -1;
$(function () {
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
});
$("#FeedbackForm").submit(function (event) {
if($("#User_Email").val())
{
check().done(function(){
console.log('execution begin');
if (ErrorEmail == 2) {
if (confirm('Are you sure this is the email you want to use?')) {
$(form).submit();
}
}
else if (ErrorEmail == 0)
{
$(form).submit();
}
});
event.preventDefault();
}
else
{
console.log('no email');
event.preventDefault();
}
});
});
function check(callback) {
var dfrd1 = $.Deferred();
console.log('checks start!');
dfrd1.resolve(
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
}).done()
);
console.log('checks complete! ErrorEmail='+ErrorEmail);
return dfrd1.done().promise();
}
// while the lookup is performing
function validation_in_progress() {
$('#status').html("<img src=@Url.Content(@"~/Assets/img/loading.gif") height='16'/>");
}
// if email successfully validated
function validation_success(data) {
//var dfrd1 = $.Deferred();
//dfrd1.resolve(data);
console.log('validation begin! ErrorEmail=' + ErrorEmail);
$('#status').html(get_suggestion_str(data['is_valid'], data['did_you_mean']));
if (data['is_valid'] && !data['did_you_mean']) {
ErrorEmail = 0;
}
else if (data['is_valid'] && data['did_you_mean']) {
ErrorEmail = 2;
}
else
ErrorEmail = 1;
console.log('validation complete! ErrorEmail=' + ErrorEmail);
//return dfrd1.promise();
}
// if email is invalid
function validation_error(error_message) {
$('#status').html(error_message);
}
// suggest a valid email
function get_suggestion_str(is_valid, alternate) {
if (is_valid) {
ErrorEmail = 0;
var result = '<span class="success">Address is valid.</span>';
if (alternate) {
result += '<span class="warning"> (Though did you mean <em>' + alternate + '</em>?)</span>';
ErrorEmail = 2;
}
return result
} else if (alternate) {
ErrorEmail = 1;
return '<span class="warning">Did you mean <em>' + alternate + '</em>?</span>';
} else {
ErrorEmail = 1;
return '<span class="error">Email address is invalid. Please try another.</span>';
}
}
我希望序列如下所示:
checks start!
validation begin! ErrorEmail=2 //this will update the ErrorEmail var.
validation complete! ErrorEmail=2
checks complete! ErrorEmail=2
execution begin
我已经搜索并尝试了所有技术(异步假/延迟/回调),但我似乎无法找出可能出现的错误
我的代码如下:
checks start!
Feedback:390 checks complete! ErrorEmail=2
Feedback:347 execution begin
Feedback:404 validation begin! ErrorEmail=2
Feedback:419 validation complete! ErrorEmail=2
var ErrorEmail = -1;
$(function () {
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
});
$("#FeedbackForm").submit(function (event) {
if($("#User_Email").val())
{
check().done(function(){
console.log('execution begin');
if (ErrorEmail == 2) {
if (confirm('Are you sure this is the email you want to use?')) {
$(form).submit();
}
}
else if (ErrorEmail == 0)
{
$(form).submit();
}
});
event.preventDefault();
}
else
{
console.log('no email');
event.preventDefault();
}
});
});
function check(callback) {
var dfrd1 = $.Deferred();
console.log('checks start!');
dfrd1.resolve(
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
}).done()
);
console.log('checks complete! ErrorEmail='+ErrorEmail);
return dfrd1.done().promise();
}
// while the lookup is performing
function validation_in_progress() {
$('#status').html("<img src=@Url.Content(@"~/Assets/img/loading.gif") height='16'/>");
}
// if email successfully validated
function validation_success(data) {
//var dfrd1 = $.Deferred();
//dfrd1.resolve(data);
console.log('validation begin! ErrorEmail=' + ErrorEmail);
$('#status').html(get_suggestion_str(data['is_valid'], data['did_you_mean']));
if (data['is_valid'] && !data['did_you_mean']) {
ErrorEmail = 0;
}
else if (data['is_valid'] && data['did_you_mean']) {
ErrorEmail = 2;
}
else
ErrorEmail = 1;
console.log('validation complete! ErrorEmail=' + ErrorEmail);
//return dfrd1.promise();
}
// if email is invalid
function validation_error(error_message) {
$('#status').html(error_message);
}
// suggest a valid email
function get_suggestion_str(is_valid, alternate) {
if (is_valid) {
ErrorEmail = 0;
var result = '<span class="success">Address is valid.</span>';
if (alternate) {
result += '<span class="warning"> (Though did you mean <em>' + alternate + '</em>?)</span>';
ErrorEmail = 2;
}
return result
} else if (alternate) {
ErrorEmail = 1;
return '<span class="warning">Did you mean <em>' + alternate + '</em>?</span>';
} else {
ErrorEmail = 1;
return '<span class="error">Email address is invalid. Please try another.</span>';
}
}
var ErrorEmail=-1;
$(函数(){
$(“#用户_电子邮件”).mailgun_验证程序({
api_键:“x”,
in_progress:validation_in_progress,//在向验证器发出请求时调用
success:validation\u success,//在验证程序返回时调用
错误:validation\u error,//发生到达验证程序的错误时调用
});
$(“#反馈表单”).submit(函数(事件){
if($(“#用户电子邮件”).val()
{
check().done(函数()){
log('executionbegin');
如果(ErrorEmail==2){
如果(确认('您确定这是您要使用的电子邮件吗?')){
$(表单).submit();
}
}
else if(ErrorEmail==0)
{
$(表单).submit();
}
});
event.preventDefault();
}
其他的
{
console.log(“无电子邮件”);
event.preventDefault();
}
});
});
函数检查(回调){
var dfrd1=$.Deferred();
log('checks start!');
解析(
$(“#用户_电子邮件”).mailgun_验证程序({
api_键:“x”,
in_progress:validation_in_progress,//在向验证器发出请求时调用
success:validation\u success,//在验证程序返回时调用
错误:validation\u error,//发生到达验证程序的错误时调用
}).完成
);
log('检查完成!ErrorEmail='+ErrorEmail');
返回dfrd1.done().promise();
}
//在执行查找时
功能验证正在进行中(){
$('#status').html(“”);
}
//如果电子邮件成功验证
功能验证成功(数据){
//var dfrd1=$.Deferred();
//解析(数据);
log('validation begin!ErrorEmail='+ErrorEmail');
$('status').html(获取建议(数据['is'u valid'],数据['did'u you'u mean']);
如果(数据['is_valid']&&!数据['dod_you_意指]){
ErrorEmail=0;
}
else if(数据['is_valid']&&data['dod_you_意指]){
ErrorEmail=2;
}
其他的
ErrorEmail=1;
log('validation complete!ErrorEmail='+ErrorEmail');
//返回dfrd1.promise();
}
//如果电子邮件无效
功能验证错误(错误消息){
$('#status').html(错误消息);
}
//建议一封有效的电子邮件
函数get\u suggestion\u str(是否有效,可选){
如果(是否有效){
ErrorEmail=0;
var result='地址有效';
如果(备选){
结果+='(虽然你的意思是“+alternate+”?);
ErrorEmail=2;
}
返回结果
}否则,如果(备用){
ErrorEmail=1;
return“您是指“+alternate+”?”;
}否则{
ErrorEmail=1;
return“电子邮件地址无效。请尝试其他地址。”;
}
}
check()
函数已经有了一个延迟的对象($('User\u Email').mailgun\u validator
)唯一要做的事情就是。管道该函数的结果并调用解析dfrdl
函数,最后只返回dfrdl如下:
function check(callback) {
var dfrd1 = $.Deferred();
console.log('checks start!');
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
}).pipe(function(res){
dfrd1.resolve(res);
})
console.log('checks complete! ErrorEmail='+ErrorEmail);
return dfrd1;
}
check()
函数已经有了一个延迟的对象($('User\u Email').mailgun\u validator
)唯一要做的就是。管道该函数的结果并调用解析dfrdl
函数,最后只返回dfrdl如下:
function check(callback) {
var dfrd1 = $.Deferred();
console.log('checks start!');
$('#User_Email').mailgun_validator({
api_key: 'x',
in_progress: validation_in_progress, // called when request is made to validator
success: validation_success, // called when validator has returned
error: validation_error, // called when an error reaching the validator has occured
}).pipe(function(res){
dfrd1.resolve(res);
})
console.log('checks complete! ErrorEmail='+ErrorEmail);
return dfrd1;
}
您遇到了什么错误?嘿@Amy,我希望顺序如下(确保在执行表单之前先验证电子邮件):检查开始!验证开始!ErrorEmail=2验证完成!ErrorEmail=2检查完成!ErrorEmail=2执行开始如果您希望事情按顺序运行,则需要将事情放入回调函数中-例如,您的检查完成应放入邮枪的完成中validator@Pete你介意举个例子吗?谢谢!上厕所k在你的check.done函数上面你得到了什么错误?嘿@Amy,我希望序列如下(确保在执行表单之前先验证电子邮件):检查开始!验证开始!ErrorEmail=2验证完成!ErrorEmail=2检查完成!ErrorEmail=2执行开始如果您希望事情按顺序运行,则需要将事情放入回调函数中-例如,您的检查完成应放入邮枪的完成中validator@Pete你介意举个例子吗?谢谢!上厕所在你的支票上签字