Domanda

I have an jQuery AJAX call that looks as follows:

$.get(url, { tripName: tripName, userRowId: userRowId }, function (data) {
    $("#partialView").html(data);
}).success(function () {
    UpdateCount();
    $("#LocationList").focus();
}).fail(function (error) {
    alertify.alert("Call to get sightings failed: " + error);
});

The .get calls a C# controller method. I've got the C# method set up to throw a divide by zero exception for testing purposes. How do I trap the exception message coming from the C# method? What I have above isn't working (the fail method gets called but no message is being displayed) This isn't surprising because I just took a stab at it.

My C# method looks as follows:

try
{
    int a = 0, b = 10, c;

    c = b / a;
}
catch (System.Exception exception)
{
    throw;
}
È stato utile?

Soluzione

change this:

alertify.alert("Call to get sightings failed: " + error);

to this:

alertify.alert("Call to get sightings failed: " + error.responseText);

If you could set a console.log(error) there you can see your error message is hold by responseText so that should get you with error.responseText.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top