js的ajax
2021-09-27 本文已影响0人
小溪流jun
<!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>