How to set header Ajax in web worker?

Hi all,

i have a worker.js for ajax call, and i need set header xhr.setRequestHeader("x-api-version", "1.0.1");.
it working in chrome browser but not running in webOS platform,
in my server, response data ready set Access-Control-Allow-Headers:Origin, X-Api-Version, Content-Type, Accept, and it working in chrome browser, but when i install app in webOS TV LG, it not working and return error:
XMLHttpRequest cannot load http://apitv.clip.vn/smart_tv/menu/8/subs. Request header field x-api-version is not allowed by Access-Control-Allow-Headers. worker.js:50 on-message.

code here:

var httpBuildQuery = function(params) { if (typeof params === 'undefined' || typeof params !== 'object') { params = {}; return params; } var query = ''; var index = 0; for (var i in params) { index++; var param = i; var value = params[i]; if (index == 1) { query += param + '=' + value; } else { query += '&' + param + '=' + value; } } return query; }; on-message = function(e) { console.log('WORKER: Message received from main script ', e.data); //try{ var data = e.data; if(data.method && data.url){ var xhr; if(typeof XMLHttpRequest !== 'undefined') { xhr = new XMLHttpRequest(); xhr.open(data.method, data.url, false); xhr.setRequestHeader("Access-Control-Allow-Headers","Origin, X-Api-Version, Content-Type, Accept"); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("x-api-version", "1.0.1"); xhr.on-readystatechange = ensureReadiness; function ensureReadiness() { if (xhr.readyState < 4) { return; } if (xhr.status !== 200) { return; } if (xhr.readyState === 4) { //console.log('WORKER RESPONSE:', xhr.responseText); postMessage(xhr.responseText); } } if(typeof data.params !=='undefined'){ xhr.send(httpBuildQuery(data.params)); }else{ xhr.send(''); } } }else{ console.log('WORKER: INVALID data to process'); } /*}catch(e){ console.log('WORKER: ERROR parse data in worker',e); }*/ };

if i using AjaxSource enyo (not in worker) it run both chrome browser and webOS TV:

var params = { content_id: this.idDetail, content_type: this.typeDetail, action: action }; var ajax = new Ajax({ url: Config.ws_endpoint + 'favorite/make', method: 'POST', handleAs: "json", postBody: params, headers:{ 'X-Api-Version': '1.0.1' } }); ajax.go(); ajax.response(this, 'processResponse'); ajax.error(this, 'processError');

Please help me fix it. Thanks

Comments

Sign In or Register to comment.