简体   繁体   English

如何对bitstamp发出HTTP请求?

[英]How do I make an HTTP request to bitstamp?

I'm trying to use an API from bitstamp to fetch a currency trading price on my webpage. 我正在尝试使用bitstamp中的API来获取我的网页上的货币交易价格。

I have researched this problem, but I still cannot get it to work as it always returns ERROR 我已经研究过这个问题,但我仍然无法让它工作,因为它总是返回ERROR

The link used is https://www.bitstamp.net/api/ticker/ and the response should be last 使用的链接是https://www.bitstamp.net/api/ticker/ ,响应应该是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");
} }

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> 

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>
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");
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在环境Dialogflow-&gt;实现中发出http请求? - How do I make a http request in the environment Dialogflow -> Fulfillment? 如何在 Javascript 中发出异步 HTTP 请求? - How do I make an asynchronous HTTP request in Javascript? 如何根据 curl 请求在 Node 中发出 http 发布请求? - How do I make a http post request in Node based off of a curl request? 如何为我向API发出的每个HTTP请求添加作为用户名包括的api令牌 - how do I add api token included as a username for each HTTP request i make to an API 如何取消 HTTP fetch() 请求? - How do I cancel an HTTP fetch() request? 如何向Angular2站点发出HTTP请求(用于抓取)? - How do I make a HTTP request (for scraping purposes) to an Angular2 site? 如何使我的PHP API将JSON返回到Javascript HTTP POST / GET请求? - How do I make my PHP API return JSON to a Javascript HTTP POST/GET request? 如何使我的侧边栏导航子菜单在Laravel中的新HTTP请求上保持展开和激活状态? - How do I make my sidebar navigation child menus remain expanded and active on new HTTP request in Laravel? 如何将此 CURL 请求转换为普通的 HTTP 请求 Javascript - How do I convert this CURL request to a normal HTTP request Javascript 如何使用代理发出发布请求 - How do I make a post request with proxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM