如果我运行此链接https://api.onwater.io/api/v1/results/10,10 API说如果此点(纬度10°N;经度10°E)在水中或土地上。
这种情况下的结果是:
{"lat":9.999237824938984,"lon":10.000257977613291,"water":false}
我如何打印价值water
??
非常感谢
答案 0 :(得分:1)
通常您可以通过其属性名称访问它:
const response = {"lat":9.999237824938984,"lon":10.000257977613291,"water":false}
console.log(response.water);
答案 1 :(得分:1)
假设您正在寻找一个AJAX调用,您可以使用纯JS这样做
function callAjax() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4
if (xmlhttp.status == 200) {
var response = JSON.parse(xmlhttp.responseText);
document.getElementById("myDiv").innerHTML = response.water;
}
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", "https://api.onwater.io/api/v1/results/10,10", true);
xmlhttp.send();
}
callAjax();
<div id="myDiv"></div>
使用jquery就像这样
$.ajax({
url: "https://api.onwater.io/api/v1/results/10,10",
context: document.body,
success: function(data){
console.log(data.water);
}
});
答案 2 :(得分:1)
假设您通过AJAX检索数据
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState === 4 && this.status === 200){
// parse the response to object
var obj = JSON.parse(this.responseText);
// print it out (obj.water and obj['water'] produces the same result)
alert(obj.water);
console.log(obj['water']); // prints it in console
}
};
xhr.open("GET", "https://api.onwater.io/api/v1/results/10,10", true);
xhr.send();
您可以详细了解AJAX here。