3

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?

Community
  • 1
  • 1
aerych
  • 51
  • 1
  • 3
  • Edited to specify I'm using OSX. – aerych Dec 17 '12 at 15:14
  • I had the chance to try this on the Windows version of the simulator and XHR works as expected. It seems that the OSX simulator preview is just buggy. – aerych Dec 17 '12 at 15:17
  • When you were installing your application were you zipping it and using the other manifest as you are required to in order for the privileged type to work? – David Zorychta Jan 13 '13 at 19:18

4 Answers4

7

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});
benfrancis
  • 391
  • 2
  • 4
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.

arekf
  • 61
  • 3
0

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/

André Bolinhas
  • 157
  • 1
  • 2
  • 10
0

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"
    }
}
dagur
  • 432
  • 4
  • 16