嗨,我正在尝试从一个API中提取一些东西,它应该会返回给我一个字符串,上面有Ethereum最近的价格。
在此之后,我希望解析字符串并删除所有数据,以便只返回最新的价格。
这是我到目前为止所拥有的代码,但是它没有返回任何内容,而且我在这方面和如何解析代码方面都陷入了困境。
任何帮助都是非常感谢的!谢谢。
{
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.kraken.com/0/public/Ticker?pair=ETHEUR', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
}
}
};
发布于 2017-12-12 06:09:18
你不是在发送请求。您需要添加xhr.send();
来发送请求。这是示例请求。
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.kraken.com/0/public/Ticker?pair=ETHEUR', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log(this.responseText);
}
};
xhr.send();
发布于 2017-12-12 06:16:33
在创建xhr并向其添加适当的回调之后,请确保调用xhr.send()
。来自该端点的响应似乎是一个JSON对象,因此您可以在响应中调用JSON.parse()
将其转换为您可以使用的javascript对象。
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.kraken.com/0/public/Ticker?pair=ETHEUR', true);
xhr.onreadystatechange = function() {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// Parse JSON response
var data = JSON.parse(xhr.responseText);
// Use the object however you wish
console.log(data);
}
}
xhr.send();
发布于 2017-12-12 06:16:35
必须调用xhr.send();
函数才能实际发送请求。否则,您只是初始化了请求,并设置了回调函数来处理响应,但是没有发送对API的请求。
https://stackoverflow.com/questions/47766218
复制相似问题