0

I have code like this:

<script>
var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function () {
    if(xhr.readyState === 4 && xhr.status === 200) {
      var data = JSON.parse(xhr.responseText);
      var ex_rate = data["market_price_usd"];
      document.getElementById('btcusd777').innerHTML = ex_rate;
    }
  };
  xhr.open('GET', 'https://blockchain.info/ru/stats?format=json');//&cors=true doesn't help
  xhr.send();
</script>
1 BTC = <span id="btcusd777"></span> USD

But it doesn't work because api is on different domain. Is there a way to fix it somehow? I don't want to use jquery

4
  • 1
    Read the first line here in the docs. "Some API calls are available with CORS headers if you add a cors=true parameter to the request". (CORS stands for Cross-Origin Resource Sharing, and that's what you"re asking) Commented Aug 23, 2015 at 17:36
  • i.gyazo.com/b7fbbdf5e6ad56443caae05dd60ce33a.png if I got you right, this doesn't help Commented Aug 23, 2015 at 17:41
  • There seems to be a dedicated URL for what you want: jsfiddle.net/5rwrh712 But the value is fixed for 24 hours, not real time... :/ Edit: actually, it did just change, so maybe it's real time Commented Aug 23, 2015 at 17:54
  • thanks, this one is working, post it as answer please. Commented Aug 23, 2015 at 18:03

2 Answers 2

1

The docs state that "Some API calls are available with CORS headers if you add a cors=true parameter to the request." But that does not seem to be the case for the call you made.

There seems to be another call that allows cross origin requests:

https://blockchain.info/ru/q/24hrprice

So just adjust your call like this:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        document.getElementById('btcusd777').innerHTML = xhr.responseText;
    }
};
xhr.open('GET', 'https://blockchain.info/ru/q/24hrprice');
xhr.send();
1 BTC = <span id="btcusd777"></span> USD

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

Comments

0

It doesn't seem to support CORS, so if you don't have control over the API development, that I guess it's the case, the only way to do it is by proxying it through your own web server.

If you have an nginx: https://www.nginx.com/resources/admin-guide/reverse-proxy/

If you are on apache: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Note that jquery or any other javascript framework will not help with this as it's a browser restriction. It doesn't seem to have crossdomain.xml file so flash will not help either.

2 Comments

obviously at first I was using my server for that, but it got banned by cloudflare after a few requests - that's why I decided to move it to clients
Then there's not too much to do to call that service... I'm glad blex found an alternative one for you :-). Good job man.

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.