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

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

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

Please help me fix it. Thanks

Comments

Sign In or Register to comment.