javascript中級--ajax(2)

1、無刷新讀取數據防止緩存html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ajax</title>
    <script src="ajax.js"></script>
    <script>
        //無刷新數據讀取,ajax
        window.onload = function() {
            var oBtn = document.getElementById('btn1')
            oBtn.onclick = function() {
                alert('txt1.txt?t=' + new Date().getTime());
                ajax('txt1.txt?t=' + new Date().getTime(), function(str) {
                    alert(str);
                })
            }
        }
    </script>
</head>

<body>
    點擊後讀取txt1.txt<br/>
    <input id="btn1" type="button" name="" value="讀取">
</body>

</html>

 2、讀取jsonajax

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ajax</title>
    <script src="ajax.js"></script>
    <script>
        //請求服務器上面的數組json,ajax
        window.onload = function() {
            var oBtn = document.getElementById('btn1')
            oBtn.onclick = function() {
                ajax('data.json?t=' + new Date().getTime(), function(str) {
                    //str->[{a:1,b:2},{a:3,b:4}]
                    var arr = eval(str);
                    alert(arr[1].b);
                })
            }
        }
    </script>
</head>

<body>
    點擊後讀取txt1.txt<br/>
    <input id="btn1" type="button" name="" value="讀取">
</body>

</html>
相關文章
相關標籤/搜索