JQuery實現獲取接口提供的數據


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-1.12.4.js"></script>
    <script>
    $(function(){
        $("#btn").on("click",function(){
            $.ajax({
                // 后端程序的url地址
                url: 'http://wthrcdn.etouch.cn/weather_mini',
                // 也可以使用method,提交數據的方式,默認是'GET',常用的還有'POST'
                type: 'get', 
                dataType: 'json',  // 返回的數據格式,常用的有是'json','html',"jsonp"
                data:{ // 設置發送給服務器的數據,如果是get請求,也可以寫在url地址的?后面
                    "city":'北京'
                }
            })
            .done(function(resp) {     // 請求成功以后的操作
                console.log(resp);
            })
            .fail(function(error) {    // 請求失敗以后的操作
                console.log(error);
            });
        });
    })
    </script>
</head>
<body>
<button id="btn">點擊獲取數據</button>
</body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM