0

I have a written a function which in turn makes ajax calls. I have used this in all my pages.

    function getFunc(fullURL, successCallback, errorCallback) {
        $.ajax({
            type : 'GET',
            url : fullURL,
            async : false,
            dataType : 'json',
            success : successCallback,
            error : errorcallback   

      });
    }

And call this function as follows-

getFunc(someurl,
            function(data){
                //display data
             },
          function(xhr, textStatus, errorThrown){
                //display errors
           }
 );

This code works fine. Now I have to add some default error handling code which is for all the web pages which call getFunc(..), but I dont want to go into each web page and do it. how can I do that in getFunc(..)?

i.e I need a way to do this-

function getFunc(fullURL, successCallback, errorCallback) {
            $.ajax({
                type : 'GET',
                url : fullURL,
                async : false,
                dataType : 'json',
                success : successCallback,
                error : do some default error handling and then call errorCallback  

          });
        }
5
  • 3
    Use the ajaxError event? Commented Mar 3, 2014 at 17:21
  • If I'm understanding this right, you need to handle errors that getFunc will throw? What is getFunc throwing? Commented Mar 3, 2014 at 17:22
  • 1
    Why are you using async: false? This will lock up the entire browser until the call is completed. Commented Mar 3, 2014 at 17:24
  • That was a typo. In my actual code its async:true. Thanks for pointing it out Commented Mar 3, 2014 at 17:31
  • @AbhishekAmte: Just get rid of the async option altogether. It's not needed. Commented Mar 3, 2014 at 17:33

2 Answers 2

3

Why not pass an anonymous function to error instead of errorcallback. Have the function do stuff then call errorcallback when it's done.

function getFunc(fullURL, successCallback, errorCallback) {
    $.ajax({
        type: 'GET',
        url: fullURL,
        async: false,
        dataType: 'json',
        success: successCallback,
        error: function(xhr, textStatus, errorThrown){
            // Do whatever
            // ...

            // Then call errorCallback
            errorCallback.apply(this, arguments);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think what you mean to say is, if the error is unhandled by the caller - i.e. they don't specify a function..

Change your getFunc to this:

function getFunc(fullURL, successCallback, errorCallback) {
    $.ajax({
        type : 'GET',
        url : fullURL,
        async : false,
        dataType : 'json',
        success : successCallback,
        error : function(error) { 
             if(errorcallback)
                errorcallback(error.xhr...etc...);
             else
                catchUnhandled(error);
        }

    });
}
function catchUnhandled (error)
{   //place catch all code here }

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.