/    Sign up×
Community /Pin to ProfileBookmark

XMLHTTPRequest used in Web Worker fails in IE11

Hi, I have a piece of code that I want to execute inside a HTML5 Web Worker. The code operates perfectly in Chrome, but fails in IE 11. The failure occurs on the .open call to the XMLHTTPRequest object. The error is …

“Error occured in XMLHttpRequest: ReadyState: 0 Status:0 E: Error: Access is denied.rn Msg:Access is denied.rn”

The code I’m running is as follows. The purpose of the code is simply to make a web service call and get the return. If I run the code outside of the WebWorker, it executes without issues. But as soon as I put it inside a web worker I get the access is denied error in IE11. It will however work in Chrome perfectly. Are there some IE settings I need to be aware of? I’ve investigated the cross domain stuff I’ve seen on the net and tried the various solutions I’ve found, but no joy. The offending line is xhr2.open(method, url, true);

Any ideas would be really appreciated. I’ve wasted a couple of days on this already!

Thanks,

Derek.

var worker_fn = function(e)
{
var data = e.data;

switch (data.cmd) {
case ‘start’:

var getSpecData = function (params, url) {

var xhr;

try {
function createCORSRequest(method, url) {
var xhr2 = new XMLHttpRequest();
if (“withCredentials” in xhr2) {

// Check if the XMLHttpRequest object has a “withCredentials” property.
// “withCredentials” only exists on XMLHTTPRequest2 objects.

xhr2.open(method, url, true);

} else if (typeof XDomainRequest != “undefined”) {

// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE’s way of making CORS requests.

xhr2 = new XDomainRequest();
xhr2.open(method, url);
} else {

// Otherwise, CORS is not supported by the browser.
xhr2 = null;

}
return xhr2;
}

var myxhr = createCORSRequest(‘POST’, url);
if (!myxhr) {
throw new Error(‘CORS not supported’);
}

myxhr.setRequestHeader(‘Content-Type’, ‘application/json; charset=utf-8’);
myxhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
myxhr.setRequestHeader(‘Access-Control-Allow-Origin’,’*’);

myxhr.onreadystatechange = function () {
if (myxhr.readyState == 4 && myxhr.status == 200) {
var result = JSON.parse(myxhr.responseText);
self.postMessage(result.d);

}
else if (myxhr.readyState != 1)
{
self.postMessage(‘XMLHttpRequest: ‘ + myxhr.statusText + ‘ ReadyState: ‘ + myxhr.readyState + ‘ Status:’ + myxhr.status + ‘ E: ‘ +e+’ Msg:’+e.message); }
};

myxhr.send(params);
}
catch (e) {
self.postMessage(‘Error occured in XMLHttpRequest: ‘ + xhr.statusText + ‘ ReadyState: ‘ + xhr.readyState + ‘ Status:’ + xhr.status + ‘ E: ‘ +e+’ Msg:’+e.message);
}
};

getSpecData(data.data, data.url);

break;
default:
self.postMessage(‘Unknown command: ‘ + data.msg);
};

};

var blob = new Blob([“onmessage =”+worker_fn.toString()], { type: “text/javascript” });

var worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = function(e)
{
getFileNameSuceeded(e.data);
};

var serviceUrl = ‘http://localhost/MySite/Services/Sdk/MyService.svc/json/GetFieldData‘;
var params = ‘{“formType”:”‘ + formType + ‘”,”referenceId”:”‘ + referenceId + ‘”,”version”:”‘ + version + ‘”,”siteIdentity”:’ + siteIdentity.toJSON() + ‘}’;

worker.postMessage({‘cmd’: ‘start’, ‘url’ : serviceUrl, ‘data’ : params});

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 23.2015 — Are you using CORS or the standard same domain methods?

also where did you get

[CODE]if ("withCredentials" in xhr2) {[/CODE]
from?

Isn't it supposed to be [CODE]if ( xhr2.withCredentials ) {[/CODE]

Also please note that any code needs to be wrapped in forum tags, these are

[B][CODE[/B][B]]...[/[/B][B]CODE] [HTML[/B][B]]...[/HTML[/B][B]] [PHP[/B][B]]...[/[/B][B]PHP][/B]
×

Success!

Help @DerekB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...