Skip to content

Instantly share code, notes, and snippets.

@mikeolivieri
Created September 8, 2011 18:30
Show Gist options
  • Save mikeolivieri/1204197 to your computer and use it in GitHub Desktop.
Save mikeolivieri/1204197 to your computer and use it in GitHub Desktop.
Parse result as JSON on $.ajax error
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
if (options.parseError) {
$.Deferred(function (defer) {
jqXHR.done(defer.resolve)
.fail(function (jqXHR, statusText, errorMsg) {
var parsed = $.parseJSON(jqXHR.responseText);
defer.rejectWith(this, [jqXHR, statusText, parsed]);
});
}).promise(jqXHR);
jqXHR.success = jqXHR.done;
jqXHR.error = jqXHR.fail;
}
});
$.ajax({
type: "POST",
url: form.attr("action"),
data: form.serialize(),
dataType: "json",
parseError: true,
success: function (data) { window.location.href = data.url; },
error: function (jqXHR, textStatus, errorMsg) {
console.log(errorMsg); //now json
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment