我對html和css很滿意,但絕對沒有使用javascript的經驗。如何在html中顯示Json API數據?
我想要顯示從該API收到之日到HTML頁面
http://api.travelpayouts.com/data/routes.json?token=PutHereYourToken
這是我怎麼沒有任何成功嘗試,直到如今。
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON('https://api.travelpayouts.com/data/routes.json?token=mytoken').then(function(data) {
alert('Your Json result is: ' + data.result);
result.innerText = data.result;
}, function(status) {
alert('Something went wrong.');
});
<body>
<div class="container">
<div id="result" style="color:red"></div>
</div>
</body>
謝謝:)
https://stackoverflow.com/help/how-to-ask – tech2017
HTTPS ://developer.mozilla.org/en-US/docs/AJAX – Thiatt
@sloane一旦你已經顯示出一些努力。一旦你已經證明你已經真正嘗試過自己,這是我們的工作。你沒有在這裏做過。 – Carcigenicate