ES6之promise天气案例
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                ES6之promise天气案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            <p><span class="add"></span><span class='weather'></span><span class='temp'></span><img src="" class="img" width="30"></p><script>$(function(){// 獲取網絡請求 信息 (根據不同的url返回不同的結果)function getApi(url){//返回一個承諾return new Promise((resolve,reject)=>{$.ajax({url:url,dataType:"jsonp",success:function(res){resolve(res);},error:function(err){reject(err)}})})}//獲取地址var urlA = "https://apis.map.qq.com/ws/location/v1/ip?key=CAABZ-AVSAQ-RDR5L-GTBDJ-HLA4O-A5FDB&output=jsonp&_=1593478015329";var urlB = "https://wis.qq.com/weather/common?weather_type=observe|forecast_24h|air&source=pc";//添加&province=省份&city=城市getApi(urlA).then(add=>{//獲取地址console.log(add);var info=add.result.ad_info;//具體的地址信息$('.add').text(info.city);return getApi(urlB+`&province=${info.province}&city=${info.city}`)}).then(data=>{//獲取天氣console.log(data);$('.weather').text(data.data.observe.weather);$('.temp').text(data.data.observe.degree)$('.img').attr("src",`https://mat1.gtimg.com/pingjs/ext2020/qqindex2018/dist/img/weather/${data.data.observe.weather_code}.svg`)}).catch(err=>{//失敗console.error(err);})})
                            
                        
                        
                        總結
以上是生活随笔為你收集整理的ES6之promise天气案例的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 深度学习入门笔记(四):向量化
 - 下一篇: 穿山甲的广告聚合平台-GroMore