ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajax</title>
</head>
<body>
<ul>

</ul>
<script src="js/jQuery3.3.1.js"></script>
<script>
    $(function () {
        $.ajax(   //ajax 是jquery封装好的一个事件
            {url: "https://free-api.heweather.com/s6/weather/now?location=changping,北京&key=83a29964479c488d8b42d884bbf0fcb0",  //访问服务器获取数据
             type: "get", // 访问方式
             success:function (data) { //请求成功作用域
                 let arr = data.HeWeather6[0].now;
                 console.dir(arr);
                 Object.keys(arr).forEach(function (item) {   //遍历对象 }{字典}
                     $(`<li>${item }: ${arr[item]}</li>`).appendTo("ul")   //使用forEach
                     })
             },
                 error: function (data) { //请求失败作用域
                     console.log(data)
                 }
            }
            )
    })
</script>
</body>
</html>

 

posted @ 2018-11-15 21:28  python传言  阅读(120)  评论(0编辑  收藏  举报