-1

I noticed that I can add a success section in .ajax. Is there any way to add a failure section when I use jQuery?

$('#signUpSubmit').click(function() 
{
    //alert("signup completed");
    var email=document.getElementById('signUpEmail').value;
    var password = document.getElementById('signUpPassword').value;
    $.ajax({
        url: 'signup.php',
        type: 'POST',
        data: {
            email: email,
            password: password
        },
        success: function() {
            alert('Email Sent');
        }               
    });
});

Edit
I try to use following but then my submit button is not active although the password are same

col-form-label">Password:</label>
                                            <input type="password" class="form-control" id="signUpPassword" name="password" onchange="check_pass()">
                                        </div>
                                        <div class="form-group">
                                            <label for="pwd" class="col-form-label">Confirm Password:</label>
                                            <input type="password" class="form-control" id="signUpConPassword" name="password" onchange="check_pass()">
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-primary" id="signUpSubmit" disabled="true" >Sign Up</button>

                                </div>
                            </div>
                        </div>
                    </div>

<script type="text/javascript">
    function check_pass() 
    {
        //alert(document.getElementById('signUpPassword').value);
    if (document.getElementById('signUpPassword').value ==
            document.getElementById('signUpConPassword').value) {
        document.getElementById('signUpSubmit').disabled = false;
    }
    else
    {
        document.getElementById('signUpSubmit').disabled = true;
    }
}
$('#signUpSubmit').click(function() 
{
    //alert("signup completed");
    var email=document.getElementById('signUpEmail').value;
    var password = document.getElementById('signUpPassword').value;
    $.ajax({
        url: 'signup.php',
        type: 'POST',
        data: {
            email: email,
            password: password
        },
        success: function() {
            alert('Email Sent');
        }      
        error: function()
        {
            alert('something is wrong');
        }         
    });
});
</script>
  • `ajax` is not a particular implementation but a general purpose technology. Check the corresponding library documentation to see if it's required and what it is for. – zerkms Apr 23 '18 at 02:26
  • 1
    [Just read the docs?](http://api.jquery.com/jquery.ajax/) – gre_gor Apr 23 '18 at 04:09
  • Seriously, jQuery has good documentation. A simple search would have given you all the information you need. It would have taken less time than writing up a question here. – Makyen Apr 23 '18 at 05:43
  • 1
    Possible duplicate of [How to get the jQuery $.ajax error response text?](https://stackoverflow.com/questions/1637019/how-to-get-the-jquery-ajax-error-response-text) – Makyen Apr 23 '18 at 05:52

3 Answers3

0

Try “ error: function(){ ... } “

Yhlas
  • 411
  • 3
  • 5
  • 17
  • @moyen it should work as I verified from docs. Also this question should not even be here, because all you had to do is to look at documentation. – Yhlas Apr 23 '18 at 13:07
  • link the documentation where this is said. –  Apr 23 '18 at 13:21
0

There is an error section , which catches bad request or other failure cases , try

error: function() {
        alert('this is error');
    } 

Also if you use complete :, it will have both success and error cases. Complete execute after the susses or error .

parlad
  • 1,143
  • 4
  • 23
  • 42
0
$.ajax({
    url: 'signup.php',
    type: 'POST',
    data: {
        email: email,
        password: password
    },
    success: function() {
        alert('Email Sent');
    }   
}).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
}); 

You may try .fail after the ajax and it provides error code such as 404,500 as well.

Anfath Hifans
  • 1,588
  • 1
  • 11
  • 20
Wils
  • 1,178
  • 8
  • 24