js 就天气预报接口调用 实例

实例

 $.ajax({
            type: 'GET',
            url: 'https://www.tianqiapi.com/api/',
            data: 'version=v1&city=上海',
            dataType: 'JSON',
            error: function () {
                alert('网络错误');
            },
            success: function (res) {
                var aa='<li>City: ' + res.city + '</li><li>Weather: ' + res.data[0].wea + '</li><li>Tips: ' + res.data[0].air_tips + '</li>';
                console.log(aa);
                var bb=1;
                // 遍历数组
                for (var i = 0; i < res.data[0].hours.length; i++) {
                   bb+='<li>' + (i + 1) + ': 时间: ' + res.data[0].hours[i].day + ' 气温: ' + res.data[0].hours[i].tem + ' </li >';
                }
                 console.log(bb);
            }
        });

摘抄事例

var getNJWeather = function(){
    var mainBox = dojo.byId('portal_weather');
    mainBox.innerHTML="正在加载天气信息,请稍候...";
    var jsonpArgs = {
        //地址
        url:  'http://weather.news.qq.com/js/gn_24.js',
        //超时时间
        timeout: 10000,
        //阻止缓存
        preventCache: true,
        load: function(data) {
            if(data){
                var weather = v.find("南京").w;
                if(weather){
                    weather = weather.replace(/城市:|\<br\/>|天气:|温度:|风力:/g," ");
                    mainBox.innerHTML = weather;
                }else{
                    mainBox.innerHTML = "加载的天气信息有误";
                }
          }else{
              mainBox.innerHTML = "没有加载到天气信息";
          }
       },
       error: function(error) {
          mainBox.innerHTML = "加载天气信息超时";
       }
    };
    //使用dojo的jsonp调用
    dojo.io.script.get(jsonpArgs);
 };
 dojo.addOnLoad(function(){
    getNJWeather();
 });

API
https://www.kancloud.cn/ccjin/yingq/603579

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值