Dojo addOnUnload和xhrGet

I have function to unlock data sets via an API

function unlockData() {

    var xhrArgs = {
        url: "/api/unlockData",
        handleAs: "json",
        content: {
            account_id: accountId
          },
        load: function(data) {
            if(data) {
                alert("Data unlocked");
            } else {
                alert("Something went wrong.");
            }

        },
        error: function(error) {
           alert("error:"+error);
        }
    }

    var deferred = dojo.xhrGet(xhrArgs);

}

which is called onUnload

dojo.addOnUnload(window, "unlockData");

When reloading, this I get this error message:

Error: Unable to load /api/unlockData?account_id=981782 status:0

Can I fire an xhrGet request on unload at all? BTW, the function works fine, if not called on unload.

Since Xhr calls are asynchronous, putting this call during the 'Unload' will fail always because the browser is not in this page anymore.

Anyways, since the addOnUnload is triggered during the window.onbeforeunload, you can put a "timer" loop to check if the AJAX call has success.

I strongly don't recommend this practice. Using an AJAX call during the "window" closing is totally unnecessary, you should consider to use a form instead and call your scripts synchronously