Search code examples
jqueryjsonjsonpfirefox-os

JSON cross domain without a jsonp server


I'm making a FireFoxOS packaged app. I want to get a library from a public json-api server, but the server that offers the services does not change the json to jsonp (with ?callback or ?jsonp still return just a json) is there a workaround on the client side? Im using jquery or zepto and backbone My code:

$.ajax({
    url: apiMangaeden[0],
    dataType: 'jsonp',
    data: 'data',
    jsonp: 'callback',
    success: function (data) {
        console.log('It works')
    }
});

Solution

  • If it's not your HTTP server and it doesn't support CORS or JSONP, you need to request the cross-site request permission (systemXHR permission) and set "type": "privileged" in your manifest.webapp:

    https://github.com/mozilla-b2g/gaia/blob/master/apps/communications/manifest.webapp#L81

    https://developer.mozilla.org/en-US/Apps/Developing/Packaged_apps#Types_of_packaged_apps

    and set the mozSystem property on the XHR object:

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Non-standard_properties

    https://github.com/mozilla-b2g/gaia/blob/master/apps/communications/contacts/js/fb/fb_query.js#L37

    On jquery >= 1.5.1 you should be able to set it using the xhrFields setting on $.ajax:

    http://api.jquery.com/jQuery.ajax/

    $.ajax({
      url: apiMangaeden[0],
      dataType: 'json',
      data: 'data',
      xhrFields: {
        mozSystem: true
      },
      success: function (data) {
        console.log('It works')
      }
    });