1
  <!doctype html>
  <html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

  <div id="test"></div>

  <script>
  (function() {
    var flickerAPI = "https://api.cryptowat.ch/markets/bitstamp/btcusd/price";
    $.getJSON( flickerAPI )
      .done(function( data ) {
        alert("price: " + data['result'].price + " cost: " + data['allowance'].cost + " remaining: " + data['allowance'].remaining);
      })
      .fail(function() {
        alert("FAIL!");
      });
  })();
  </script>
</body>
</html>

In IE show returned data in other browser this code return "FAIL!".
In firefox, I use firebug and checked returned data: "CORS header 'Access-Control-Allow-Origin' missed".
How to set Access-Control-Allow-Origin and correct code to work in Firefox?

Ivica
  • 11
  • 3

1 Answers1

0

You should probably review some CORS tutorials/introductions. A great one is: https://www.html5rocks.com/en/tutorials/cors/

Having said that, the short answer is that the provider of the API needs to allow Cross domain access. Something like the following in an apache config: Header set Access-Control-Allow-Origin *

Unfortunatly, not something you can do much about. In some situations switching to 'JSONP' as the dataType works. In your case, you could change your call to $.getJSON to something like

$.ajax({
   url: flickerAPI,
   dataType: 'JSON',
   jsonpCallback: 'callback',
   type: 'GET',
})

However, this requires that the server knows how to respond to JSONP, and it looks like cryptowat.ch does not.

Zoe
  • 27,060
  • 21
  • 118
  • 148