<!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('请求成功', data);
},
status => {
console.log('请求失败', status);
}
).catch(e => {
console.log('异常', e);
});
</script>
</head>
<body>
</body>
</html>