Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display error returned by $.ajax call?

Tags:

jquery

I have a code that takes forever to load, and finally when I put error handler it shows alert, but I need to know what error it returned? How can I know?

EDIT: I get requested url not found, but I am certain the url: is a valid URL on my host, what could be wrong? I can even access it directly in browser.

// process logging in a user from sidebar
$("#login-form").submit(function(event) {
    $('input:submit').attr("disabled", true);
    $("p.form-result").empty();
    $('p.form-submit').after('<p class="loading"><img src="<?php bloginfo('template_directory'); ?>/img/loading.gif" alt="" /></p>');
    $.ajax({
        url: '<?php bloginfo('template_directory'); ?>/ajax/login.php',
        type: 'POST',
        data: $(this).serialize(),
        dataType: 'json',
        success: function(data){
            $('.loading').remove();
            $('input:submit').attr("disabled", false);
            if (data.status) {
                // success
                $("p.form-result").html('<span class="success">' + data.message + '</span>');
                window.setTimeout(function(){location.reload()},3000);
            } else {
                // error
                $("p.form-result").html('<span class="error">' + data.message + '</span>');
            }
        },
        error: function(data){
            alert('error');
        }
    });
    return false;
});
like image 272
Ahmed Fouad Avatar asked Jul 11 '12 12:07

Ahmed Fouad


People also ask

How do you check if AJAX call is completed?

The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

How do I return a response from AJAX?

Hello @kartik, What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.

What does an AJAX call return?

The $.ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.


2 Answers

The error event of the jQuery function $.ajax receives 3 arguments

error : function(jqXHR, textStatus, errorThrown){

}

This is the jQuery documentation for this event :

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

You'll we be able to know what error is with the parameter textStatus

like image 97
jbrtrnd Avatar answered Oct 06 '22 20:10

jbrtrnd


Error event in ajax call get executed when ajax call has some invalid arguments in it. Following function will help you to understand error code by throwing an error and displaying error definition in alert.

error: function(xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                },
like image 9
Nikhil Saswade Avatar answered Oct 06 '22 18:10

Nikhil Saswade