0

So I have this website that has JSON object and trying to have it as an object.

As most people do once it comes to javascript problem, I took a look into jQuery and found out about awesome function called .getJSON(). (Example is below, and one can replace file name with URL)

$.getJSON('ajax/test.json', function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});

Which is not that hard to figure out... But problem is, below statement.

  • Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

Considering my page and the source page is not from same source, above method is not a possible option for me.

Is there any other way to solve this problem?

Thank you in advance.

Anatoli
  • 922
  • 2
  • 11
  • 25
  • Do you control the source of the data? – RightSaidFred Dec 01 '11 at 02:10
  • 1
    possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – hugomg Dec 01 '11 at 02:15
  • While I do agree that above thread can possible solve my issue, my question is broader. My question isn't limited to overcoming same-origin policy, but getting and interpreting JSON object from different URL. – Anatoli Dec 01 '11 at 02:24

1 Answers1

0

From the docs:

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.

So you need to change your url slightly to read like http://path/to/resource?callback=?

This only works if the resource supports JSONP, which means the server will wrap the data in a function call corresponding to your callback parameter, which jQuery will then reference in a script tag. If you can't use JSONP, your only option is some sort of server-side proxy.

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107