JSFiddle - React, Tailwind, and code Playground
JavaScript
// Install the prefilter once and for all
jQuery.ajaxPrefilter(function( options, _, jqXHR ) {
if ( options.coldfusion ) {
// Create normalizing deferred
var dfd = $.Deferred();
// Have normalizing deferred listening to actual promise
jqXHR.then( dfd.resolve, function() {
if ( jqXHR.status === 500) {
// Normalize the fail() response.
dfd.rejectWith( this, [ {
success: false,
data: "",
errors: [ "Unexpected error." ],
statusCode: xhr.status
}, "error", jqXHR ] );
} else {
// Normalize the non-500 "failures."
dfd.rejectWith( this, [ $.parseJSON( jqXHR.responseText ),
"success", jqXHR ] );
}
});
// Replaces actual promise with normalizing one
dfd.promise( jqXHR );
jqXHR.success = jqXHR.done;
jqXHR.error = jqXHR.fail;
}
});
// Use coldfusion normalization for a specific request
jQuery.ajax( url, {
coldfusion: true
} );
// Use colfusion normalization for all requests
jQuery.ajaxSetup({
coldfusion: true
});