I am trying to create a global handler that is called before the ajax callback. I make a lot of ajax calls with my application, and if this is an error, I return a certain structure, so I need to run something before success works to check the response data to see if it contains an error code bit, e.g. 1 / 0
Response example
{"code": "0", "message": "your code is broken"}
or
{"code": "1", "data": "return some data"}
I canβt find a way to do this in jQuery out of the box, looked at prefilters, ajaxSetup and other available methods, but they didnβt quite pull it out, the rates I could come up with is to hack ajax itself a bit:
var oFn = $.ajax; $.ajax = function(options, a, b, c) { if(options.success) { var oFn2 = options.success; options.success = function(response) {
I have been using this for a while and it works great, but it was interesting if there is a better way to do this or something that I missed in jQuery docs.
source share