Skip to content

A monkey patch for Dojo's XHR method to globally handle response status codes, for example server session timeouts (401 unauthorized etc)

Notifications You must be signed in to change notification settings

der-tale/Dojo-XHR-response-code-handling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Handle response codes globally with Dojo's xhr()

This small js file monkey patches the Dojo's original xhr function to globally and synchronously handle server side responses with different status codes other than 200.

For example, if your server session has timed out, this patch allows you to display a login screen and after a successful authentication to automatically re-send the original xhr call to resume the application without any further interruption.

How to use

Download the single js file and include it after dojo.js and before your application code:

<script src="js/dojo/dojo.js" type="text/javascript" charset="utf-8"
    djConfig="..."></script>

<script src="js/dojoXhrExtension.js"></script>

<script src="js/yourappcode.js"></script>

Before making any xhr calls or initializing any Dijit components which could do that, define your handler functions this way:

dojo._handleXhrStatus[401] = {
    retryAfterwards: true, 
    handler: function() {
        var d = new dojo.Deferred()
    
        dojo._xhr("GET", {
            url: "auth/signIn",
            content: {
                username: "administrator",
                password: "god"
            }
        }).then(function(result) {
            d.resolve()
        }, function() {
            d.reject() 
        })

        return d
    }
}

If any xhr call gets the response 401, your configured function is invoked. While you handle the event, any other xhr call is paused until you resolve/reject the Deferred.

You can handle 401s, but also any 5xx code for example. If you cannot resume normal operations afterwards, set retryAfterwards to false to inhibit the xhr call blocking.

Contact / Licence

I haven't found any other "official" way to accomplish this. So if you have any comments, recommendations or have found any bugs, please do not hesitate to drop me an email.

This code is licenced under the terms of LGPL Version 3.

About

A monkey patch for Dojo's XHR method to globally handle response status codes, for example server session timeouts (401 unauthorized etc)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published