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( "json", function( options ) {
options.dataTypes.shift();
options.dataTypes.unshift( "json", "test" );
console.log( options.dataTypes );
});
// 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
});