1

I'm trying to work with JSON data from API. Some JSON I can get, and then work with them. But sometimes I have fail: JSON doesn't recognised.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
  </head>
  
  <body>
    <script>
      $.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(json_data0) {
        alert(JSON.stringify(json_data0));
      });
      
      $.getJSON('https://graph.facebook.com/btaylor', function(json_data1){
        alert(JSON.stringify(json_data1));
      });

      $.getJSON('https://btc-e.com/api/2/ltc_usd/ticker', function(json_data2) {
  alert(JSON.stringify(json_data2));
   });
    </script>
  </body>
</html>

QUESTION: Why does last api doesn't get by getJSON? The similar situation is for example for https://www.bitstamp.net/api/ticker/

Marat
  • 327
  • 1
  • 2
  • 10
  • what do you mean by couple? and why is that sometimes you happen to get data and other times not? is that server problem or client side? – Abhinav Gauniyal May 07 '15 at 09:26
  • 1
    Job one for any issue in JS, check the console: `XMLHttpRequest cannot load https://btc-e.com/api/2/ltc_usd/ticker. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.` You're being stopped by the Same Origin Policy – Rory McCrossan May 07 '15 at 09:27
  • The first two work, but I get a cross-origin error on the third one : `No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stackoverflow.com' is therefore not allowed access. `So... well, you're just not allowed to pull this json. – Jeremy Thille May 07 '15 at 09:28
  • so, is it ok, that via browser i can see json, but i cant get it to my html page? – Marat May 07 '15 at 09:31
  • Also remember `$.ajaxSetup({ crossDomain: true; });` – mplungjan May 07 '15 at 09:32
  • And, on top of the other issue: The response header of the third API has the Content-Type set to `text/html` rather than `application/json`, which might also let `getJSON` fail – devnull69 May 07 '15 at 09:40

2 Answers2

1

You need to use CORS request or JSONP. In this case, I think that the best for you is to use JSONP because yahoo apis offer it. Please, read a similar and complete answer that I wrote in the past: local AJAX-call to remote site works in Safari but not in other browsers

Community
  • 1
  • 1
Jose Mato
  • 2,709
  • 1
  • 17
  • 18
1

The answer is right in the console log ;)

XMLHttpRequest cannot load https://btc-e.com/api/2/ltc_usd/ticker. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

This service does not allow other domains to call their API's. This is a security feature built into browsers which you cannot disable.

If they wanted to allow access, they would have to add the proper CORS headers to their response:

Access-Control-Allow-Origin: *

Read more about CORS here: http://enable-cors.org/

Another possibility is to create a proxy server yourself that would fetch this data and deliver to your browser.

Guilherme Rodrigues
  • 2,818
  • 1
  • 17
  • 22