the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Hidden Ajax Errors with jQuery

    Jul 5 2007

    If you use Ajax with jQuery, you may have noticed that you don't get any error messages when things go wrong. Even if you have major bugs in your callback functions, jQuery just silently fails, sweeping any errors under the rug, and leaving you clueless as to what just happened.

    For example, you can do this and you won't get any errors:

    $.get('page.html', function(){
        this_function_does_not_exist();
    });
    

    There are two ways to fix this. You can use $.ajax with the error parameter, passing an error handling function for that particular Ajax call. Or, you can define a global Ajax error handler. If you do a lot of Ajax, and you use Firebug, the latter is a great option. Try this:

    $(document).ajaxError(function(){
        if (window.console && window.console.error) {
            console.error(arguments);
        }
    });
    

    After running this code, you'll start getting error messages in your Firebug console (if anything breaks with your Ajax calls or callbacks). The error messages aren't the greatest, but at least you don't have to stay in the dark any longer.

    Tags: javascript jquery ajax
    View 11 Comments | Add a comment
  • Comments

    1. Jörn Zaefferer at 2:25pm on July 6, 2007

    That's a great tip. That generic error handler inside jQuery's ajax method can be quite annoying otherwise. I wonder if these two lines should go into jQuery core...

    2. Andy Cai at 7:25am on July 11, 2007

    like the jquery.

    3. jonnix at 9:15pm on November 8, 2007

    Thank you very much. This tip helped me find an error I have been guessing for during 2 days.

    4. Acronyms at 9:25am on April 2, 2008

    I used similar way, put creating a separate function.

    5. colin at 11:42pm on May 8, 2008

    Thank you, this idea just saved me a lot of guess work

    6. Braden at 1:55am on May 15, 2008

    I'm confused--this doesn't seem to work with 1.2.3. Did they at one point try { success() } catch() { handleError(); }? I know Dojo does that, but I don't see it in 1.2.3 or 1.0.0.

    7. Mat at 4:00pm on July 27, 2008

    Thanks for the tip :)

    8. Vlad at 6:25pm on September 2, 2008

    Thank you!

    9. LaPeGa at 8:51pm on November 26, 2008

    Thanxxx

    10. Ramiro at 10:06pm on March 6, 2009

    Great Tip!!! specially the debug to console using firebug. Thanks

    11. zoltantoth at 10:59pm on May 10, 2009

    his is exactly what I was searching for. You could not explain it simplier.

    Thank you.

    P.s.:
    I would draw your attention to the error in the comment engine:
    If I use a single quote, you are double escape-ing it, so it throws an SQL error, so the shortened "could not" will not work.

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • jQuery Live Events
    • I need web developers
    • buttons need type="submit" to submit in IE
    • Win $200 in a Web Dev Writing Contest
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (39)
    • links (21)
    • about (19)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (6)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
  • Older Articles

    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2009 The Future of the Web