Javascript JS-从API请求打印JSON信息
我有这个功能。通过xhr.response,我得到了我的json值。问题:如何将这些JSON值附加到HTML标签?多谢各位 不相关的问题:有没有人知道一个好的网站,在那里可以简洁地总结JSON的概念 例如,从国家/地区到标签国家/地区Javascript JS-从API请求打印JSON信息,javascript,html,json,Javascript,Html,Json,我有这个功能。通过xhr.response,我得到了我的json值。问题:如何将这些JSON值附加到HTML标签?多谢各位 不相关的问题:有没有人知道一个好的网站,在那里可以简洁地总结JSON的概念 例如,从国家/地区到标签国家/地区 function initPage(){ var xhr = new XMLHttpRequest(); xhr.open("GET", "http://ip-api.com/json", false); xhr.send();
function initPage(){
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://ip-api.com/json", false);
xhr.send();
console.log(xhr.status);
console.log(xhr.statusText);
console.log(xhr);
console.log(xhr.response);
}
JSON输出:
拜访
所需的HTML:
<label id="landcode"></label>
<label id="country"></label>
<label id="regio"></label>
<label id="city"></label>
<label id="postcode"></label>
<label id="latitude"></label>
<label id="longitude"></label>
<label id="ip"></label>
您正在执行
XMLHttpRequest()
调用,需要检查状态==200
(2xx成功/200确定)和readyState=4
(完成):
更改内容
在XMLHttpRequest调用之前
函数initPage(){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“国家”).innerHTML=
这个.responseText;
}
};
xhr.open(“GET”http://ip-api.com/json“,假);
xhr.send();
控制台日志(xhr.status);
console.log(xhr.statusText);
console.log(xhr);
console.log(xhr.response);
}
JSON看起来像什么?您希望HTML最终看起来像什么?您可能还想阅读和理解添加的JSON输出和所需的html@MattBurland@SomeName乐意帮忙