JSFiddle - React, Tailwind, and code Playground
JavaScript
// OPEN YOUR CONSOLE =)
// Set up converter that runs when the
// dataType is "json test"
$.ajaxSetup({
converters: {
"json test": function( data ) {
return "Went through converter";
}
}
});
// Setup ajax prefilter adds " test" to the dataType if the dataType is "json"
$.ajaxPrefilter(function( options ) {
if ( options.dataType == "json" ) {
console.log("Modifying the dataType on the fly for request number " + options.number );
options.dataType += " test";
console.log("dataType changed to 'json test' for request number " + options.number + "?", options.dataType == "json test");
}
});
// The first request should hit the converter because
// it has the dataType of "json test",
// and it does. \o/
$.ajax({
url : "/echo/json/",
dataType: "json test",
type: "POST",
success: function( data ) {
console.log( "Request 1", data );
},
number: 1
});
// The second request should hit the converter because it's
// dataType is "json" and is being dynamically changed to
// "json test" in the ajax prefilter, but it doesn't. /o\
//
// The data in the success callback should be "Went through converter",
// not an empty object.
$.ajax({
url : "/echo/json/",
dataType: "json",
type: "POST",
success: function( data ) {
console.log( "Request 2", data );
},
number: 2
});