jQuery的AJAX调用时间太长或东西 [英] jquery ajax call taking too long or something
问题描述
我有,我要确保PayPal的电子邮件地址是有效的之前,我提交表单。所以,我想提出一个jQuery的提出像这样的电话
I have a form that I want to ensure the paypal email address is valid before I submit. So i am making a jquery submit call like this
$('#new_user').submit(function(){
$.ajax({
type: 'post',
url: "/validate_paypal",
dataType: 'json',
data: {email : $('#user_paypal_email').val()},
success: function( data ) {
if (data.response["valid"] == false){
$('#user_paypal_email').closest('.field').addClass('fieldWithErrors');
$('#user_paypal_email').append('<span style="color:#E77776;">This is not a valid email address</span>');
return false;
}else{
return true;
}
}
});
但问题是,这一呼吁多数民众赞成在第二和页面已经刷新AJAX完成之前....如果我把返回的错误在通话结束时,我可以看到我的JSON是正确的,但由于某种原因,这样我现在不会结束......如何纠正任何想法
but the problem is this call thats a second and the page already refreshes before the ajax is complete....if I put the return false at the end of the call I can see my json is correct but for some reason the way I have it now wont finish...any ideas on how to correct this
推荐答案
就在()立即停止使用,preventDefault当提交事件。然后等待来自PayPal的反应,然后调用提交()的形式。
Just use preventDefault() immediately when the submit event is fired. Then wait for the response from paypal and then call submit() on the form.
$('#new_user').submit(function(e){
e.preventDefault();
var form = $(this); //save reference to form
$.ajax({
type: 'post',
url: "/validate_paypal",
dataType: 'json',
data: {email : $('#user_paypal_email').val()},
success: function( data ) {
if (data.response["valid"] == false){
$('#user_paypal_email').closest('.field').addClass('fieldWithErrors');
$('#user_paypal_email').append('<span style="color:#E77776;">This is not a valid email address</span>');
return false;
}else{
form.unbind('submit'); //remove binding
form.submit(); //submit form
}
}
});
这篇关于jQuery的AJAX调用时间太长或东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!