1

I'm trying to use an API from bitstamp to fetch a currency trading price on my webpage.

I have researched this problem, but I still cannot get it to work as it always returns ERROR

The link used is https://www.bitstamp.net/api/ticker/ and the response should be last

Here is my code:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.bitstamp.net/api/ticker/", true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
     var response = JSON.parse(xhr.responseText);
     window.alert(response.last);
}
else {
    window.alert("ERROR");
} }
  • if I am not wrong, you should be seeing below error in your browser console - "No 'Access-Control-Allow-Origin' header is present on the requested resource" – Naga Sai A Sep 30 '17 at 19:25
  • Yeah this is true. But how can I deal with it – Trrrrrrrrrr Sep 30 '17 at 19:33
  • possible duplicate https://stackoverflow.com/questions/28547288/no-access-control-allow-origin-header-is-present-on-the-requested-resource-err – Naga Sai A Sep 30 '17 at 19:40
  • Please edit your question to include all of the error details contained in the developer console, and the full error message from bitstamp. Those are essential parts of an HTTP request question. – Graham Sep 30 '17 at 20:05

3 Answers3

1

Try this:

function loadXMLDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    var jsonRes= JSON.parse(this.responseText);
      if (jsonRes.hasOwnProperty('last')) {
                 document.getElementById("demo").innerHTML =
  jsonRes.last;
                 alert(jsonRes.last);
}
    }
  };
  xhttp.open("GET", "https://www.bitstamp.net/api/ticker", true);
  xhttp.send();
}
<h2>Using the XMLHttpRequest object</h2>

<button type="button" onclick="loadXMLDoc()">Change Content</button>
<p>last attribute is: <span id="demo"></span></p>
Melchia
  • 22,578
  • 22
  • 103
  • 117
1

Here is one way:

<script src="./jquery.min.js">
//none secure web page ?
    jQuery.get("https://www.bitstamp.net/api/ticker/", function (data, status)
    {
        // use response here; jQuery passes it as the first parameter
        var response = JSON.parse(data);
        window.alert(response.last);
            console.log("MyFunc: " + "response : " + response + "\nStatus: " + status);
    });

</script>
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
  • It seems correct, but it doesn't work... what am I doing the wrong way? – Trrrrrrrrrr Sep 30 '17 at 19:37
  • It is correct @Trrrrrrrrrr ;O). must be your URL, try "http://www.bitstamp.net/api/ticker/" (taken off the s of https), hmm it does not have a none secure web page, I tried it, only secure https . – Jon Goodwin Sep 30 '17 at 19:39
  • Have you lost internet access or having problems with your firewall ? – Jon Goodwin Sep 30 '17 at 19:52
1
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.bitstamp.net/api/ticker/", true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);

function processRequest(e) { 
  if (xhr.readyState == 4) {
    if (xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      window.alert(response.last);
    } else {
      window.alert("ERROR");
    }
  }
}
Kostas
  • 1,903
  • 1
  • 16
  • 22
  • Your problem is in your logic. First check if request is completed (readyState == 4) and only after check the status-code and print your desired value or ERROR. – Kostas Sep 30 '17 at 20:27