views:

45

answers:

3

I have the following jQuery which does not give the most descriptive error messsages...

url: 'server_page.aspx',
type: 'POST',
data: { intID:$(this).attr("id"), strState:"1" },
error: function() { alert('Error'); },
success: function() {  }

How do I get more descriptive error messages if it is possible?

EDIT:

This is the full javascript:

$(document).ready(function(){ $("input:checkbox").change(function() { var that = this;

    if($(this).is(":checked")) { 
        $.ajax({
            url: 'favorite_on_off.aspx',
            type: 'POST',
            data: { strFavoriteID:$(that).attr("id"), strState:"1" },
            timeout: 1000,
            error: function(xhr, status, error)
            {
                alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "1");
                alert('Error: ' + status + '\nError Text: ' + error);  
            },
            success: function() {  }
        });
    } else {
        $.ajax({
            url: 'favorite_on_off.aspx',
            type: 'POST',
            data: { strFavoriteID:$(that).attr("id"), strState:"0" },
            timeout: 1000,
            error: function(xhr, status, error)
            {
                alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "0");
                alert('Error: ' + status + '\nError Text: ' + error);  
            },
            success: function() {  }
        });
    }
}); 

});

These are the error messages:

values: strFavoriteID: c:\folder\document.doc strState: 1

Error: error Error Text: undefined

+1  A: 

The second argument provided to the error callback is textStatus, which should contain a description of the error:

error: function(xhr, textStatus) { alert(textStatus); }

Note that you should probably not provide this information to your users. Parse the message using Javascript and give them a nice friendly message explaining the error.

lonesomeday
Using the `xhr` object, you will also be able to access the actual response returned from the server. `xhr.responseText` can give you the text of the .NET error page, your 404 page, or a 500 page. This can be very useful information.
Ryan Kinal
@ lonesomeday, this is just giving me "error" in a alert box.
oshirowanen
This isn't the main argument you want to use, as the actual error values are only null, "timeout", "error", "notmodified" and "parsererror"...it's better to include the third argument as well for much more detail in many cases.
Nick Craver
@oshirowanen - try `alert(xhr.responseText)` or `$('#debug').html(xhr.responseText)`, with a `<div id="debug">...</div>` in your document.
Ryan Kinal
I've updated the original question to show the full script plus error messages.
oshirowanen
+2  A: 

You can use all of the arguments passed to the error callback, for example:

error: function(xhr, status, error) { 
  alert('Error: ' + status + '\nError Text: ' + error); 
},
Nick Craver
This is giving me an alert message as follows "Error: error Error Text: undefined".
oshirowanen
@oshirowanen - What do you get with `alert('Error: ' + status + '\nError Text: ' + error + '\nResponse Text: ' + xhr.responseText);`? Also, this isn't the only debug method, using your console, or Firebug or Chrome's net panel are all good tools for seeing what's blowing up.
Nick Craver
Trying this out now.
oshirowanen
@Nick Craver, thanks, that gave me enough info to see where the problem is.
oshirowanen
A: 

I have this method:

function HandleAjaxError(request, status, error) {
    var ex = eval("(" + request.responseText + ")");

 $('body').addClass("ui-widget-overlay");
    alert(ex.Message);
    $('body').removeClass("ui-widget-overlay");
}

$.ajax({
    type: "POST",
    url: window.location.pathname + "/DoStuff",
    data: "{}",
    success: Success,
    error: HandleAjaxError
});

This way, I can gracefully handle the error message (and possibly the status code) on the ASP.NET side. I usually log the original error, then throw a custom/descriptive one for the client.

Rafael Belliard