分享下知心天气jsonp跨域访问的代码
Posted smallteeth
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分享下知心天气jsonp跨域访问的代码相关的知识,希望对你有一定的参考价值。
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跨域访问的代码的主要内容,如果未能解决你的问题,请参考以下文章