0

In My page i am sending data to server side using 8 ajax call...

I don't want to handle ajax error for each and every ajax call......

Single ajax error handle all the ajax error in entire page....

is their any inheritance is possible for the entire page..

 function SendConfirmationEmail(ShipmentID, ChannelOrderReference) {
var Url = '<%=Url.Action("SendShipmentEmail","Shipments") %>';
$.ajax({
    cache: false,
    type: "POST",
    data: 'strOrderShipmentId=' + ShipmentID + '&channelOrderReference=' + ChannelOrderReference,
    url: Url,
    datatype: "HTML",
    success: function (data) {
        if (data == "1") {
            SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'Email is successfully sent for Order#' + ChannelOrderReference + '');
        }
        if (data == "-2") {
            SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'Email Template is not Choosen for this Store');
        }
        if (data == "-1") {
            SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'Problem in Sending Email for Order#' + ChannelOrderReference + '');
        }
        if (data == "0") {
            SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'Connection Failed to Send Email for Order# ' + ChannelOrderReference + '');
        }
        if (data == "-3") {
            SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'ShipTo Email Address is Not Given for Order# ' + ChannelOrderReference + '');
        }
        // SucessErrorMessageDisplay('DivStatus', 'lblStatus', 'imgStatus', 0, 'Order# :' + ChannelOrderReference + ' is voided successfully');

    },
    error: function (xhr, ajaxOptions, thrownError) {
        if (xhr.status == 403) {
            window.location.href = '<%: Url.Action( "SessionExpire", "Home" ) %>';
        }
    }
});

}

2 Answers 2

2
$.ajaxError(function myErrorHandler(e, xhr, options, thrownError) {
  alert("Ajax error!");
});

Should make the trick.

Sign up to request clarification or add additional context in comments.

Comments

0

Hopefully I understand your question...

When you're calling JQuery's ajax, you're passing in an object with several properties. One of these properties is error. JQuery expects a function to be assigned to this property. You're supplying an anonymous function, but there's no reason why this can't instead be a reference to a function.

So you could define a function elsewhere in your script something like this:

function handleError(xhr, ajaxOptions, thrownError) {
   if (xhr.status == 403) {
      window.location.href = '<%: Url.Action( "SessionExpire", "Home" ) %>';
   }
}

and then in your ajax calls, reference this:

$.ajax({
    cache: false,
    // etc...
    error: handleError
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.