Html 使用公共API从站点提取JSON数据并将其异步显示在页面上
Html 使用公共API从站点提取JSON数据并将其异步显示在页面上,html,json,xml,Html,Json,Xml,src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> 了解天气情况 城市: 温度: 速度: 湿度: 最低温度: 最高温度: 函数转换温度(val){ 变量温度=((val-273.15)*9)/5)+32; 返回温度; } $(文档).ready(函数(){ $(“按钮”)。单击(函数(){ var zipcode=$(“#zipcode”).val(); $.ajax({ 方法:“获取”, url:“ht
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
了解天气情况
城市:
温度:
速度:
湿度:
最低温度:
最高温度:
函数转换温度(val){
变量温度=((val-273.15)*9)/5)+32;
返回温度;
}
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
var zipcode=$(“#zipcode”).val();
$.ajax({
方法:“获取”,
url:“http://api.openweathermap.org/data/2.5/weather?zip=“+zipcode
+“,us&appid=b6f0657aa7b18c98e70c7bee5d36f1df”,
数据类型:“json”
}).完成(功能(结果){
var response=JSON.parse(结果);
$('#city').text(response.name);
$(“#temparature”).text(convertTemp(response.main.temp));
$(“#速度”).text(响应.风速);
$(“#湿度”).text(response.main.湿度);
$('#mintemparture').text(convertTemp(response.main.temp_min));
$('#maxtemparature').text(convertTemp(response.main.temp_max));
});
});
});
需要解决的几个问题:
- 在
标记中,您添加了一个额外的
。你可以把它改成
- 调用api时,请使用
而不是https
http
- 您不需要执行
var response=JSON.parse(result)代码>,因为设置
已经为您解析了它。因此,试图调用对象上的数据类型:“json”
会抛出一个错误。相反,只需直接使用JSON.parse
等即可result.name
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>