Today I got around the CORS restrictions on Firefox OS and managed to download JSON from an external server.
Here is my code.
I had to use a bunch of different sources, including this "boilerplate" app I found that shows how to use a bunch of different APIs. I just searched for 'json' in the repo and found this code, which helped a lot. I also found this stack overflow discussion, where someone mentioned passing in the parameters { mozSystem: true } to the line:
var xhr = new XMLHttpRequest({ mozSystem: true });
This property, along with adding the permission "systemXHR" to my manifest.webapp, allowed me to bypass the CSP.
I haven't done any handling of the JSON, but calling xhr.response returns a json object that contains an array with a bunch of book data in it, so we are now in a position where this stuff can be handled!
Calling xhr.books[0] returns the json object containing all the data we need for this particular entry. I'm not sure why it is structured as an object within a single element array within another object though. That seems kind of odd.
One thing I've found to be helpful is to declare a global variable like this one here and then setting the thing you are trying to debug to the global variable so that you can go into console and explore the methods for _xhr. There's probably a better way to do this, though. I just don't know what it is.
Nice post! Again, this will be most helpful when we meet with the Mozilla mentors. Your clearly written and thorough documentation should make mentoring much easier.
ReplyDelete