分享下知心天氣jsonp跨域訪問的代碼

來源:互聯網
上載者:User

標籤:llb   val   hash   get   cal   dev   html   hmac   class   

 var myWeather = (function(){        var funShun = "https://api.thinkpage.cn/v3/weather/now.json?location=Fushun&callback=showWeather&"        return {            el : {},            init : function(){                var that = this;                $.extend(that.el,{                    ‘nowWeather‘ : $(‘.now-weather‘),                    ‘nowTemperature‘ : $(‘.now-temperature‘)                })            },            getUrl : function(){                var time = Math.round(new Date().getTime()/1000);                var str = "ts=" + time + "&ttl=30&uid=UF4C86B626";                var hash = CryptoJS.HmacSHA1(str, "nvczde0rd9tkkr3o");                var base = hash.toString(CryptoJS.enc.Base64);                var sig = encodeURIComponent(base);                 var url = funShun  + str + "&sig=" + sig;                return url;            },            getWeather : function(){                var that = this;                console.log(that.el.nowWeather)                $.ajax({                    url:that.getUrl(),                    jsonp : ‘callback‘,                    jsonpCallback : ‘showWeather‘,                    success : function(data){                       function showWeather(data){                            that.el.nowWeather.html(data.results[0].now.text)                            that.el.nowTemperature.html(data.results[0].now.temperature)                            $.cookie(‘weather‘, JSON.stringify(data))                       }                       eval(data);                    }                })            },            rundev : function(){                this.init();                if($.cookie(‘weather‘) !=‘‘){                    var weather =  $.parseJSON($.cookie(‘weather‘))                    this.el.nowWeather.html(weather.results[0].now.text)                    this.el.nowTemperature.html(weather.results[0].now.temperature)                }else{                    this.getWeather();                }            }        }    })()    myWeather.rundev();

  

分享下知心天氣jsonp跨域訪問的代碼

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.