0

I'm trying to get a JSON response from Kraken api to convert currences, but all the methods that I found (also here in stackoverflow), though they work for other sites (like "https://api.cryptonator.com/api/currencies" or the one discussed in the stackoverflow thread "/questions/12460378/how-to-get-json-from-url-in-javascript"), they don't work at all with kraken (example "https://api.kraken.com/0/public/Assets"), I don't get any response, as the URL is broken, but by accessing it with the browser I can clearly see the JSON object.

I'm using pure javascript because I'm working in wordpress, but if necessary I can add jquery (thought I would continue with js in order to not waste other time).

Untill now, I tried:

    function httpGet(theUrl)
{
    var xmlHttp = null;

    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}    
prove = eval('(' + httpGet("https://api.kraken.com/0/public/Assets") + ')');

Working with both cryptonator and yahoo (the 2nd example), but not with kraken.

    var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };
    xhr.send();
};

getJSON('https://api.kraken.com/0/public/Assets',
function(err, data) {
  if (err != null) {
    alert('Something went wrong: ' + err);
  } else {
    alert('Your query count: ' + data);
  }
});

Same as before. I also tried with jquery but the result is the same, the kraken url is the only not working

$.getJSON("https://api.kraken.com/0/public/Assets", function(data) {
    // Get the element with id summary and set the inner text to the result.
    $('#summary').text(data.result);
});

I really can't understand why I can't manage to get and parse this damn JSON object from that site while the others are working well, considering also the fact that it gives me a response if I enter the link via browser.

Thanks in advance for the help

Debolito
  • 11
  • 1
  • 4
  • 2
    It's because that API doesn't have the `Access-Control-Allow-Origin: *` header set so it doesn't allow requests via Ajax. – JJJ Mar 14 '17 at 09:49
  • As @JJJ has mentioned, if you open up your dev tools on your browser of choice (mine being Chrome) you'll get an error explaining why you cannot fulfil your request. `XMLHttpRequest cannot load https://api.kraken.com/0/public/Assets. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.` – Dan Mar 14 '17 at 09:52
  • As @JJJ mentioned, the domain you're calling is not configured to allow requests via JS code (by including CORS headers in the response). You can see this from the console error in [this fiddle](https://jsfiddle.net/gzf0cvja/). To fix this you either need to add the CORS headers - assuming you have access to the api.kraken.com domain - or instead make your request from server side code instead – Rory McCrossan Mar 14 '17 at 09:52
  • So by doing the request from a PHP script/page instead of js shoud it works without this CORS problem? – Debolito Mar 14 '17 at 10:58

1 Answers1

0

The main problem is:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

If you want to use jsonp, the server must have a jsonp api. Click here to see the server supports.

zyfyy
  • 323
  • 3
  • 10