跨域请求:查询天气

例:http://tianqi.2345.com/t/shikuang/57083.js?1507707799100
response:var weatherinfovar = {"weatherinfo":{"city":"\u90d1\u5dde","cityid":"101180101","temp":"13","SD":"82%","WD":"\u4e1c\u5317\u98ce","WS":"1\u7ea7","QY":"1026","JS":"\u964d\u6c34\u6982\u7387: 59%","pm25":"22","idx":"39","lv_hint":"\u4f18","aqiLevel":1}};

解析后:weatherinfo:
JS: "降水概率: 59%"
QY: "1026"(气压)
SD: "82%" (湿度)
WD: "东北风"
WS: "1级"
aqiLevel: 1
city: "郑州"
cityid: "101180101"
idx: "39"
lv_hint: "优"
pm25: "22"
temp: "13"(当前温度)
__proto__: Object

分析url:‘57083’是城市的编号,?1507707799100:时间戳

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>跨域请求2</title>
    <script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="http://tianqi.2345.com/t/map_js/china.js"></script>
    <script type="text/javascript" src="http://tianqi.2345.com/t/shikuang/57083.js" id="change"></script>
    <script type="text/javascript">
        //weatherinfovar:郑州天气  chinaWeaInfo:全国天气
        console.log(weatherinfovar);
        //获取各省会城市的cityid 和 name,并转换为json对象
        var cityidinfo ="{";
        $.each(chinaWeaInfo,function(index,item){
            cityidinfo += '"' + item.name +'"'+ ':' +'"'+ index +'"' +',';
        });
        cityidinfo = cityidinfo.slice(0,-1) + "}";
        cityidinfo = JSON.parse(cityidinfo);

        weather = weatherinfovar.weatherinfo;
        $(function () {
            weatherinfo(weather);
            $('#search').click(function () {
                //console.log($('#change').attr('src'));
                sendrequest(cityidinfo[$('#cityid').val()])
            })
        });
        //显示天气
        function weatherinfo(weather) {
            var msg = '';
            msg += '城市: ' + weather.city +'<br>';
            msg += '当前温度: ' + weather.temp + '℃<br>';
            msg += '湿度: ' + weather.SD + '<br>';
            msg += '风力: ' + weather.WD + weather.WS + '<br>';
            msg += '气压: ' + weather.QY + 'hPa<br>';
            msg += '空气质量: ' + weather.lv_hint;
            $('#weatherinfo').html(msg)
        }

        //发送ajax请求
        function sendrequest(cityid) {
            var src;
            //设置请求url
 src = "http://tianqi.2345.com/t/shikuang/" +cityid+ ".js?t=" + new Date().getTime(); $('#change').attr({'src':src}); //$.getJSON(src+'format =json&jsoncallback=?'); $.ajax({ url:src, dataType:'jsonp', complete:function (xhr,status) { console.log(status); console.log(weatherinfovar); weatherinfo(weatherinfovar.weatherinfo); } }); } </script></head><body><input type="text" name="cityid" id="cityid"><input type="button" value="查询" id="search"><br><div id="weatherinfo"></div></body></html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值