I want to make ajax request from Web Worker. But when making request from Emulator the not getting the ready state change event but when trying from Web Simulator its showing 'Access-Control-Allow-Origin' error : Origin file:// is not allowed by Access-Control-Allow-Origin.
The code as follows:
WH.ajax({
url : 'http://localhost/www/test/test.php',
method : 'GET',
success : function(data) {
// some codes here
}
});
/* Worker helper */
var WH;
(function() {
WH = {
merge : function(j1, j2) {
for ( var attrname in j2)
j1[attrname] = j2[attrname];
return j1;
},
ajax : function(o) {
var fData = null, c = {
method : 'GET',
async : true,
success : function(data) {
}
}
c = this.merge(c, o);
if (c.method == 'POST')
c.header = {
'Content-type' : 'application/x-www-form-urlencoded'
};
var XHR = new XMLHttpRequest();
XHR.onreadystatechange = function() {
if (XHR.readyState == 4 && XHR.status == 200) {
c.success(XHR.responseText);
}
else{
c.success('Request status :'+XHR.readyState);
}
}
XHR.open(c.method, c.url, c.async, c.username || null, c.password || null);
if (c.header)
for (k in c.header) {
XHR.setRequestHeader(k, c.header[k]);
}
if (c.data)
for (k in c.data) {
fData = fData ? '&' : '' + k + '=' + c.header[k];
}
XHR.send(fData);
}
};
})();