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/

6
  • 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? Commented May 7, 2015 at 9: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 Commented May 7, 2015 at 9: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. Commented May 7, 2015 at 9:28
  • so, is it ok, that via browser i can see json, but i cant get it to my html page? Commented May 7, 2015 at 9:31
  • Also remember $.ajaxSetup({ crossDomain: true; }); Commented May 7, 2015 at 9:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.