admin管理员组文章数量:1405859
I'm kinda new to js and API,
I have written a little code that is suppose to go ask 2 websites some data, then I only take a part of the result and add it to my html page.
I'm trying to get it work with the first website as it will be easy to make it work on the second one when I'll have the first.
I want to use only js and not php or other languages.
It is not working for the moment...
So here is my js
Ok if you e here only now,
My code has been updated using the 2 answers below of Justin and Aashah, the issue now is that the browser says that in order to fetch with cors, I need a http or https, and says that there is an issue with [object%20Object], so still no data showing up, if someone has a solution, please help us :)
var urlbitfinex = "";
var urlkraken = "=";
var resultBitfinex;
//request bitfinex price
request.get(urlbitfinex + "/pubticker/btcusd",
resultBitfinex = function(error, response, body) {
console.log(body)
return body;
});
//request kraken price
request.get(urlkraken + "xbteur",
function(error, response, body) {
console.log(body);
});
//Pushing the result to the html
var price_USD = document.getElementById('price-usd');
var USDPrice = '<p>USDEUR Price:' + resultBitfinex.ask '</p>';
price_USD.innerHTML += USDPrice;
And my html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="price-usd">
</div>
<script src="getPrices.js"></script>
</body>
</html>
I'm kinda new to js and API,
I have written a little code that is suppose to go ask 2 websites some data, then I only take a part of the result and add it to my html page.
I'm trying to get it work with the first website as it will be easy to make it work on the second one when I'll have the first.
I want to use only js and not php or other languages.
It is not working for the moment...
So here is my js
Ok if you e here only now,
My code has been updated using the 2 answers below of Justin and Aashah, the issue now is that the browser says that in order to fetch with cors, I need a http or https, and says that there is an issue with [object%20Object], so still no data showing up, if someone has a solution, please help us :)
var urlbitfinex = "https://api.bitfinex./v1";
var urlkraken = "https://api.kraken./0/public/Ticker?pair=";
var resultBitfinex;
//request bitfinex price
request.get(urlbitfinex + "/pubticker/btcusd",
resultBitfinex = function(error, response, body) {
console.log(body)
return body;
});
//request kraken price
request.get(urlkraken + "xbteur",
function(error, response, body) {
console.log(body);
});
//Pushing the result to the html
var price_USD = document.getElementById('price-usd');
var USDPrice = '<p>USDEUR Price:' + resultBitfinex.ask '</p>';
price_USD.innerHTML += USDPrice;
And my html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="price-usd">
</div>
<script src="getPrices.js"></script>
</body>
</html>
Share
edited Oct 16, 2017 at 14:27
Ilan
asked Oct 16, 2017 at 12:55
IlanIlan
4891 gold badge8 silver badges17 bronze badges
6
- mmm, seems vars not declared like "entry" and to request that isnt used for nothing – Álvaro Touzón Commented Oct 16, 2017 at 12:57
- What do you mean by "and to request that isnt used for nothing" ? – Ilan Commented Oct 16, 2017 at 13:04
- Yes its not clarity , you're using to request for nothing, there is not used for anything. – Álvaro Touzón Commented Oct 16, 2017 at 13:04
- I'm sorry but there are data in the request api.bitfinex./v1/pubticker/btcusd – Ilan Commented Oct 16, 2017 at 13:06
-
'Access-Control-Allow-Origin'
is something you cannot fix on the client side. The owner of the server needs to add that to the server side of their application. The first request doesn't work because the owner of the server needs the server to allow everyone to access it. I tried to second request and that one does work. See plnkr.co/edit/BEdYpVhJGCXyTPnAP482?p=preview and just change the urls – Jimenemex Commented Oct 16, 2017 at 14:50
2 Answers
Reset to default 1You aren't really doing anything with the response.
If you don't have to support IE you can use fetch
. Here's an example taken from:
How to get the response of XMLHttpRequest?
var url = "https://stackoverflow."; // Change this to your URL
fetch(url)
.then(function(response) {
if(response.ok) { // Check if response went through
response.json().then(function(data) {
var price_USD = document.getElementById('price-usd');
var USDPrice = '<p>USDEUR Price:' + data.something + '</p>';
price_USD.innerHTML += USDPrice;
});
} else { // Response wasn't ok. Check dev tools
console.log("response failed?");
}
});
More about Fetch
here.
You have to handle UI changes in the callback:
fetch({ url: urlkraken + "/pubticker/btcusd", cors: true })
.then(res => res.json())
.then(res => {
const price_USD = document.querySelector('#price-usd');
const USDPrice = '<p>USDEUR Price:' + res.ask + '</p>';
price_USD.innerHTML += USDPrice;
})
.catch(err => console.log(err));
本文标签: javascriptDisplay API request results in HTML page using only jsStack Overflow
版权声明:本文标题:javascript - Display API request results in HTML page using only js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744952562a2634165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论