firefox OS app,CORS in Firefox os app

左心房为你撑大大i 提交于 2019-12-10 10:50:41

问题


I have been developing web-app(not hosted app) in firefox OS . I want to access the websites xml/JSON data using XMLHttp request. but it gives error as CORS not allowed to access the data . I know about to add 'Access-Control-Allow-Origin' header in website and enabling CORS may cause security issues. But is their any alternate way to access the data feed via XMLHttp request?


回答1:


First change your manifest to have the following fields (the type one gets forgotten by people):

"type": "privileged", 
"permissions": {
    "systemXHR" : {}
}

Second, move all your JavaScript code to a separate JS file. Because it's not allowed to have inline tags in a privileged application.

Third use the mozSystem constructor like raidendev said:

var xhr = new XMLHttpRequest({ mozSystem: true });



回答2:


To perform cross-domain http request from Firefox OS app you need to set permission systemXHR in app's manifest:

 "permissions": {
     "systemXHR" : {}
 }

and create XMLHttpRequest with property mozSystem set to true:

var xhr = new XMLHttpRequest({ mozSystem: true });

Also, for any cases where XMLHttpRequest is not applicable, you can use TCP Socket API.

var socket = navigator.mozTCPSocket.open('localhost', 80);

socket.ondata = function (event) {
  if (typeof event.data === 'string') {
    console.log('Get a string: ' + event.data);
  } else {
    console.log('Get a Uint8Array');
  }
}


来源:https://stackoverflow.com/questions/25397511/firefox-os-app-cors-in-firefox-os-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!