DFOXpro
DFOXpro

Reputation: 353

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')
    }
});

Upvotes: 1

Views: 1840

Answers (3)

rpl
rpl

Reputation: 171

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')
  }
});

Upvotes: 1

Simon
Simon

Reputation: 432

You need the systemXHR permission:

https://developer.mozilla.org/en-US/Apps/Developing/App_permissions

Upvotes: 0

marti_
marti_

Reputation: 124

I recomend you CORS Proxy CORS Proxy allows javascript code on your site to access resources on other domains that would normally be blocked due to the same-origin policy.

http://www.corsproxy.com/

Upvotes: 0

Related Questions