js的ajax

作者: 小溪流jun | 来源:发表于2021-09-27 20:17 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localstorage</title>
    <style>
        .home {
            color: #000;
        }

        .box {
            width: 100px;
            height: 100px;
            background: pink;
        }
    </style>
</head>

<body>
    <div class="home">
        <div class="box">

        </div>
    </div>
    <script>
        const getJSON = function (url) {
            return new Promise((resolve, reject) => {
                const xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Mscrosoft.XMLHttp');
                xhr.open('GET', url, false);
                xhr.setRequestHeader('Accept', 'application/json');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState !== 4) return;
                    if (xhr.status === 200 || xhr.status === 304) {
                        resolve(xhr.responseText);
                    } else {
                        reject(new Error(xhr.responseText));
                    }
                }
                xhr.send();
            })
        }
        getJSON('hhtp://xxljunjun.com/jun')
    </script>
</body>

</html>

相关文章

网友评论

      本文标题:js的ajax

      本文链接:https://www.haomeiwen.com/subject/sleknltx.html