Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用公共API从站点提取JSON数据并将其异步显示在页面上_Html_Json_Xml - Fatal编程技术网

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>