ES6 Promise

page code

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            function getHtml(url) {
                return new Promise((resolve, reject) => {
                    let xhr = new XMLHttpRequest();
                    xhr.open('GET', url);
                    xhr.onload = () => {
                        resolve(xhr.responseText);
                    };
                    xhr.onerror = () => {
                        reject(xhr.statusText)
                    };
                    xhr.send();
                });
            }
            getHtml('http://www.baidu.com').then(
                data => {
                    console.log( ' The request was successful ' , data);
                },
                status => {
                    console.log( ' Request failed ' , status);
                }
            ).catch(e => {
                console.log( ' Exception ' , e);
            });
        </script>
    </head>

    <body>
    </body>

</html>

 

Chrome enables cross-domain, the console prints the result

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324989830&siteId=291194637