<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 id="weather"></h1>
<script>
var weather=document.getElementById("weather");
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("get","https://restapi.amap.com/v3/weather/weatherInfo?key=d9d5010b5236022d6f2e83252fbd62b2&&city=350100",true);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var data=JSON.parse(xmlhttp.responseText);
if(data.status==1){
weather.innerHTML=data.lives[0].city+"天气是"+data.lives[0].weather;
}else{
alert(data.info);
}
}
}
</script>
</body>
</html>
ajax实例:天气查询
最新推荐文章于 2024-06-19 23:29:50 发布