views:

205

answers:

3

Hi all,

I want add some functionality to the jquery $.ajax call. Specifically I want to trap for http error 403. I prefer not to edit jquery JS itself. I started to look at .extend but am a little confused by the examples.

Can I $.extend $.ajax() with to trap for 403?

TIA Zombie Killer.

A: 

Extend doesn't work like that. You might be better served by something like this:

jQuery.fn.myCustomAjax = function() {
    // custom code here
    jQuery.ajax(); // etc
};

The #1 major benefit of this is that it won't stuff with any external code (plugins) and will be much easier to work with when you upgrade jQuery.

nickf
This is a decent and low-risk solution if you write/control the code that invokes $.ajax(). If, on the other hand, you're trying to detect 403 in 3rd-party JS code -- such as jQuery plugins -- you need something else.
Drew Wills
@Drew - it's simple enough to save it first with something like: **$._origAjax=$.ajax;** then override the **$.ajax** function, but it's not a trivial fix in any event.
NVRAM
+1  A: 

Have you looked at jQuery.ajaxSetup options? Or at jQuery.ajaxError?

Either way, don't edit the jQuery source.

NVRAM
A: 

Thanks Guys. The suggested solution wont work becuase I need to trap for specific http result codes and then "do something special" via javascript.

so I have added 5 lines of code to jquery core library (I know dont flame me). To solve this problem in the core I suggest the following parameter would be added to jquery core libs (new parameter is responseHandlers)

$.ajax(
     responseHandlers: {
          "403":function() { http403(); } ,
          "500":function() { http500(); }
     }

global scoped:

function http403()
{
}

function http500()
{
}
Zobie Killer