$(function () { getNowArea() }) /* 定位当前城市 */ function getNowArea() { var nowCity = new BMap.LocalCity(); nowCity.get(result => { var cityName = result.name; //当前的城市名 console.log(cityName); refreshWeather(cityName); }); } /* 获取天气信息 */ function refreshWeather(cityName) { jQuery.support.cors = true; var url = encodeURI("http://wthrcdn.etouch.cn/weather_mini?city=" + cityName); //var url = "https://www.tianqiapi.com/api/?version=v6&cityid=83458415&appid=68261499&appsecret=YnD2Zs5K"; $.ajax({ url: url, type: "get", dataType: "json", async: false, success: function (data) { console.log('天气', data) } }); }