Firefox OS packaged apps and XMLHttpRequests

女生的网名这么多〃 提交于 2019-11-30 16:04:50

问题


I've been looking into packaged apps for Firefox OS and I'm relying on the Simulator since I don't have a build of the OS on a device. I'm having trouble with XHR requests and I'm not sure if the issue is a setting I'm overlooking or if the Simulator is just buggy.

My reading of the documentation suggests adding permissions for network-http should be sufficient for making XHR requests to a web service. I have also seen in source code a systemXHR permission. I've tried both but so far no joy. I'm using the simulator with Firefox for OS X and my manifest looks like so:

Manifest:

{
  "version": "0.1",
  "name": "Hello World",
  "description": "A hello world app.",
  "launch_path": "/app/index.html",
  "icons": {
    "16": "/app/img/icons/mortar-16.png",
    "48": "/app/img/icons/mortar-48.png",
    "128": "/app/img/icons/mortar-128.png"
  },
  "installs_allowed_from": ["*"],
  "permissions": {
    "systemXHR": {},
    "network-http": {},
    "network-tcp": {}
  },
  "type": "privileged"
}

A similar question has been answered, but I think the answer might have been meant for a web app, not a packaged app, and an example would helpful regarless.

Am I missing something obvious, is the simulator broken, or is CORS required for packaged apps?


回答1:


Note that to use the systemXHR permission you also need to pass a special argument when creating the request object, e.g.

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



回答2:


For Backbone or other Javascript framework using JQuery $.ajax under the hood use:

$.ajaxSetup( {
    xhr: function() {return new window.XMLHttpRequest({mozSystem: true});}
});

I don't think that you need to use CORS. My app is working fine in simulator without CORS but fails to call remote REST server if launched on local server.

Be aware that if you forgot to set

dataType: "text"

for some $.ajax calls (for example load html template) you can get XMLDocument as a result while desktop browsers returns string.




回答3:


Take a look on Rob Nyman Boilerplate, he have a working XHR demo

https://github.com/robnyman/Firefox-OS-Boilerplate-App

http://robnyman.github.com/Firefox-OS-Boilerplate-App/




回答4:


To enable CORS in my Firefox OS app, I had to enable systemXHR permission in the apps manifest.webapp file:

"permissions": {
    "systemXHR" : {
      "description" : "Required to access remote api"
    }
}


来源:https://stackoverflow.com/questions/13873025/firefox-os-packaged-apps-and-xmlhttprequests

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