Mike C
Mike C

Reputation: 3117

Why doesn't dojo.io.script.get() execute the provided error function when receiving a 404?

I am trying to use the following to do a cross-domain get:

dojo.io.script.get({
  url: myUrl,
  callbackParamName: "callback",
  preventCache: true,
  load: dojo.hitch( this, loadFunction ),
  error: dojo.hitch( this, function() {
    console.log('Error!!!');
  })
}); 

The load function runs fine, however, when the server returns a 404, the error function does not run. Can anyone tell me why?

EDIT

After some investigation, I found that a timeout and handler could be implemented in the following way:

dojo.io.script.get({
  url: myUrl,
  callbackParamName: "callback",
  timeout: 2000
}).then(function(data){
  console.log(data);
}, function(error){
  alert(error);
});

This uses functionality provided by the dojo.Deferred object.

Upvotes: 2

Views: 2484

Answers (2)

J Max
J Max

Reputation: 2381

I realize this is old but I thought I'd share a solution in case others, like I had, come across this thread.

dojo.io.script is essentially adding a <script/> to your html page. So you can try this:

    var script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', myUrl);
    script.onerror = function() {
        debugger
    }
    script.onload = function() {
        debugger
    }
    document.getElementsByTagName('body')[0].appendChild(script);

That way if the script fails to load the onerror event is called.

*This may not work in every instance but is a good start

Upvotes: 0

Eugene Lazutkin
Eugene Lazutkin

Reputation: 43956

When accessing server with script tags (that what dojo.io.script.get does), status code and headers are not available.

You may try some other ways to detect a problem, like using a timeout and analyzing a content of a script. The latter is problematic for JSONP calls (like in your example).

Upvotes: 2

Related Questions