- AJAX (Asynchronous JavaScript And XML) 就是用 JS 执行异步网络请求
- AJAX 是浏览器上的功能,浏览器可以发请求与收响应
- 浏览器在 window 上加了一个构造函数 XMLHttpRequest
- JS通过这个函数构造出的对象可以实现发请求与收响应的功能
> window.XMLHttpRequest // ƒ XMLHttpRequest() { [native code] }
> typeof window.XMLHttpRequest // "function"
使用方法
- 创建 HttpRequest对象(全称 XMLHttpRequest)
- 调用对象的 open 方法
- 监听对象的 onload 和 onerror事件(专业前端改用 onreadystatechange 事件, 因为onerror事件不会触发)
- 在事件处理函数里操作CSS内容
- 加载 CSS :
<style>
,innerHTML
- 加载 JS :
<script>
,innerHTML
- 加载 HTML :
<div>
,innerHTML
, DOM API - 加载 XML :
responseXML
, DOM API - 加载JSON :
JSON.parse
- 加载 CSS :
- 调用对象的 send 方法(发现请求)
getCSS.onclick = () => {
const request = new XMLHttpRequest(); // readyState === 0
request.open('GET', 'style.css'); // readyState === 1
request.onreadystatechange = () => {
if (request.readyState === 4) {
// 成功 2xx,失败 4xx 5xx
if (request.status >= 200 && request.status < 300) {
console.log(1, request.response);
const style = document.createElement('style');
style.innerHTML = request.response;
document.head.appendChild(style);
}else{
alert('加载失败');
}
}
}
// request.onload = () => {
// const style = document.createElement('style');
// style.innerHTML = request.response;
// document.head.appendChild(style);
// }
// request.onerror = () => {
// alert('加载失败');
// }
request.send(); // readyState === 2
}
getJS.onclick = () => {
const request = new XMLHttpRequest();
request.open('GET', '2.js');
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 300) {
console.log(1, request.response);
const script = document.createElement('script');
script.innerHTML = request.response;
document.body.appendChild(script);
}else{
alert('加载失败');
}
}
}
request.send();
}
getHTML.onclick = () => {
const request = new XMLHttpRequest();
request.open('GET', '3.html');
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 300) {
// console.log(1, request.responseXML);
const div = document.createElement('div');
div.innerHTML = request.response;
document.body.appendChild(div);
}else{
alert('加载失败');
}
}
}
request.send();
}
getXML.onclick = () => {
const request = new XMLHttpRequest();
request.open('GET', '4.xml');
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 300) {
const dom = request.responseXML;
// console.log(1, request.responseXML);
const text = dom.getElementsByTagName('warning')[0].textContent;
console.log(text.trim()); // text.trim() 去掉 text 前后的空格
}else{
alert('加载失败');
}
}
}
request.send();
}
getJSON.onclick = () => {
const request = new XMLHttpRequest();
request.open('GET', '5.json');
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status < 300) {
// console.log(request.response);
const object = JSON.parse(request.response);
// console.log(object);
ZhuzhuName.textContent = object.name;
}else{
alert('加载失败');
}
}
}
request.send();
}
readState
XMLHttpRequest.readyState 属性返回一个 XMLHttpRequest 代理当前所处的状态
一个请求的生命周期:
readState
const req = new XMLHttpRequest() ------ 0
req.open() 后 --------------- 1
req.send() 后 --------------- 2
页面出现第一个信息 --------------- 3
页面信息下载完 --------------- 4
AJAX面试代码
var request = new XMLHttpRequest();
request.open('GET', '/xx');
request.onreadystatechange = function() {
if(request.readyState === 4){
console.log('请求完成');
if(request.response.status >= 200 && request.response.status < 300) {
console.log('请求成功')
}else{
console.log(request.status)
}
}
}
request.send()