【发布时间】:2018-03-10 22:03:51
【问题描述】:
我正在尝试从 API 调用中获取对输出的响应,以便使用换行符等对其进行格式化。我尝试了解析和字符串化,但仍然只能将输出作为一行。
var HttpClient = function() {
this.get = function(url, callback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
callback(anHttpRequest.responseText);
}
anHttpRequest.open( "GET", url, true );
anHttpRequest.send( null );
}
}
var client = new HttpClient();
client.get("https://api.opendota.com/api/players/26202535/matches?limit=1",
function(response) {
JSON.parse(response);
console.log(response);
document.getElementById("demo").innerHTML=response;
});
输出: [{"match_id":3469695808,"player_slot":3,"radiant_win":true,"duration":1237,"game_mode":22,"lobby_type":7,"hero_id":13,"start_time":1506540251, "version":20,"kills":11,"deaths":2,"assists":12,"skill":3,"leaver_status":0,"party_size":1}]
【问题讨论】:
标签: javascript json api