1.首先要获取ip地址,可以自行搜索获取ip地址的相关资料,我这里就只用搜狐的的获取方法了
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
2.之后是获取天气信息的接口了,网上也能搜索到相关的资料,我这里只是用了其中的一个
https://www.apiopen.top/weatherApi?city=xxx // xxx是想要获取某地区的地名
3.完整的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="js/jquery-1.11.0.js"></script>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script>
let ip = returnCitySN["cip"];
let cityName = returnCitySN["cname"].split('省')[1].split('市')[0];
console.log(cityName)
$.ajax({
type: 'post',
url: "https://www.apiopen.top/weatherApi?city=" + cityName,
success: function (data){
console.log(data)
}
})
</script>
</body>
</html>