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

实例

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

    你可能感兴趣的:(接口)