Jump to content

Recommended Posts

I would like to run preventDefault() if the ajax call fails.

How do I set the sf variable inside of my ajax complete function?

 

$(document).ready(function() {
   
    
    $('#login').submit(function(e){
    
         var sf = true;
         
        $.ajax({type: 'POST', 
                url: '/index/check-email', 
                async: 'false', 
                data: {email: 'brianrlange@gmail.com' }, 
                complete: function(XMLHttpRequest, textStatus) {
            
                if (XMLHttpRequest.responseText == 'fail') {
                    
                    $('#norisk').css("display", "none");
                    $('p.error').css("display", "block");
                    sf = false;    
                } else {
                    alert(XMLHttpRequest.responseText);
                }
       }});
       
       
       if (!sf) {
          e.preventDefault(); 
      }
    
  });
});

Thanks for the response. I want to call e.preventDefault() based upon the results of the ajax call.

e.preventDefault() only appears to work outside of the ajax call and I can't get the result of the ajax call outside of the scope of the ajax function.

so when you do this

sf = false;

 

Nothing happenes?

 

Also you might try the $.data() method.

Just attach $(whatever).data('failed')

And check if($(whatever).data('failed')){ do your shtuff}

 

This is more of a workaround than a direct solution to you method.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.