JSFiddle - React, Tailwind, and code Playground

by sfoster

HTML

<h1>Using dojox.io.xhrPlugins to force handleAs: json </h1>

<p>See console for output. Our xhrGet uses handleAs: 'text', but the response object our load handler receives is a js object via handleAs: 'json'.</p>

<p>Note that to overide the default xhr handlers, we need to set the 4th 'override' param to true</p>

CSS

h1 { font-size: 2em; margin-bottom: 1em; }
p { padding-bottom: 0.75em; }

JavaScript

dojo.require("dojox.io.xhrPlugins");

dojo.ready(function(){
    dojox.io.xhrPlugins.register(
        "always-json",
        function(method,args){
            // optionally test some condition to apply this plugin
            return true;
        },
        function(method,args,hasBody){
            console.log("always-json plugin in effect");
            args.handleAs = "json";
            return dojox.io.xhrPlugins.plainXhr.apply(dojo, arguments);
        },
        false,
        true // highest priority
    );
    
    dojo.xhrGet({ 
        url:'/echo/json/', 
        handleAs: 'text',
        content: { foo: 'bar' }
    }).then(function(resp){
        console.log("response: ", resp);
    });
});