2

I'm trying to make global ajax handler of general purpose responses. (Eg. refresh page)

Is there any handler or hack so i'd get already parsed json, so i woudn't have to parse it twice?

$(document).ajaxSuccess(function(e, xhr) {
    // Validate and parse xhr.responseText TWICE!
});

Okay, found a bit "hacky" solution, maybe useful to others :)

Kristian
  • 3,283
  • 3
  • 28
  • 52

1 Answers1

2

Solution is to override jquery ajax json parser:

function parseJsonResponse(d) {
    var json = jQuery.parseJSON(d); // Same as default

    // Do anything with json object :)

    return json;
}
// Override original parser, defaults to jQuery.parseJSON.
jQuery.ajaxSettings.converters['text json'] = parseJsonResponse;

And if you dont want parseJsonResponse to be a global function then you can put this code in self-executing anonymous function

Community
  • 1
  • 1
Kristian
  • 3,283
  • 3
  • 28
  • 52