Mark Davidson
Mark Davidson

Reputation: 5513

Dojo Cross-Domain - Access is denied

I've got an issue when trying to use dojotoolkit cross domain.

I'm setting the headers in apache Access-Control-Allow-Origin and Access-Control-Allow-Headers which allows for it to work fine in all browsers except for IE8 and IE9. Where I get 'Access is denied' messages.

Such as

Line: 711
Character: 3
Code: 0
Error Message: Access is denied.

URL: http://remote.site.net/includes/dojotoolkit/dojo/_base/xhr.js

Can anyone spread any light on this?

Upvotes: 1

Views: 4490

Answers (2)

phusick
phusick

Reputation: 7352

Internet Explorer 8 and 9 does not support CORS via XMLHttpRequest, but via proprietary XDomainRequest. Unfortunately, Dōjō does not take this into consideration and attempts to load a cross-domain resource via XHR, which ends with the Access is denied error.

Fortunately, Dōjō provides powerful dojo/request/registry, that allows you modify this behavior:

Define XDomainRequest provider that employs XDomainRequest to obtain a resource:

function xdr(url, options) {
    var def = new Deferred();
    var xdr = new XDomainRequest();
    if (xdr) {
        xdr.onload = function(e) {
            def.resolve(xdr.responseText);
        }
        xdr.open(options.method, url);
        xdr.send();
        return def;
    }
    def.reject(new Error('XDomainRequest not supported.'));
    return def;
}

Then define corsProvider that calls XHR or XDR depending on the browser:

function corsProvider(url, options) {
    if(window.XDomainRequest) {
        return xdr(url, options);
    }
    return xhr(url, options);
}

Register corsProvider to handle cross-domain requests:

var url = "http://cors-test.appspot.com/test";
var handle = request.register(url, corsProvider);

Now requesting a cross-domain resource should work in IE too:

request.get(url).then(function(response) {
    console.log(response);
});

See it in action: http://jsfiddle.net/phusick/LZZhs/

This applies for Dōjō 1.8+, because of dojo/request. If you need the same for dojo/_base/xhr there is dojox.io.xhrPlugins, but I have no experience with it. Anyway, it should be quite straightforward to implement the aforementioned via dojo/aspect for legacy Dōjō versions.

Upvotes: 4

Royston Shufflebotham
Royston Shufflebotham

Reputation: 970

IE8 and IE9 typically don't play very nicely with this stuff unless you start using their separate XDomainRequest objects. What are you using to launch the request?

See also these other SO questions which deal with much of this:

Upvotes: 0

Related Questions