Languages

Menu
Sites
Language
Cannot make Ajax request from Web Worker

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);
}
};
})();
 

 

 

Edited by: Brock Boland on 17 Mar, 2014 Reason: Paragraph tags added automatically from tizen_format_fix module.

Responses

7 Replies
Marco Buettner
At first add on config.xml under access '*' including subdomains... Also if you try to reach a file on your app you can use /opt/apps/Appid/
Lakshmi Grandhi
Hi anfas, You cannot use Ajax inside WebWorker, you have to use native XMLHttpRequest to work in simulator. But in emulator i am getting some Signing alert , i will raise bug .
Lakshmi Grandhi
Hi anafas, Even in emulator i am getting demo_workers.js (12) :ReferenceError: Can't find variable: $ so we have to native API instead of ajax
anfas ci
I'm trying to access external url and the access in config.xml is properly set. I'm using native XMLHttpRequest is using. In the above code custom ajax class (WH.ajax) is defined. In emulator, XHR readyStates 1 & 4 is only returning. There is no response body available for the request. ------------------------------------------------------------------------------------------- Value State Description 0 UNSENT open()has not been called yet. 1 OPENED send()has not been called yet. 2 HEADERS_RECEIVED send() has been called, and headers and status are available. 3 LOADING Downloading; responseText holds partial data. 4 DONE The operation is complete. ------------------------------------------------------------------------------------------- Also there is no XHR status 200 getting with the response.
anfas ci
Once again posting the code I'm using for making ajax request: /* Worker helper */ var WH; (function () { WH = { 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); }, merge: function (j1, j2) { for (var attrname in j2) j1[attrname] = j2[attrname]; return j1; } }; })();
Lakshmi Grandhi
Hi , I have tested code its working in simulator, please try your code with other any external url. var XHR = new XMLHttpRequest(); XHR.onreadystatechange = function () { if (XHR.readyState == 4 && XHR.status == 200 ) { var msg = XHR.responseText; postMessage( msg ); } else { var msg = 'Request status :' + XHR.readyState; postMessage(msg); } } XHR.open("GET", "https://pais-dev.zypr.net/api/v2/weather/forecast/?token=14941239b3fa26429fd411e93c162d4a3f5958faa35b43456&placename=Delhi", "true" ); XHR.send();
anfas ci
It seems like there is some issue with the url I'm using when accessing from worker. I'll check it. Thank you