一些关于Ajax的零碎知识

86 阅读1分钟

浏览器渲染

1、早期的网页都是通过后端渲染完成的:服务器端渲染(SSR) image.png 2、现在的网页都是客户端渲染:前后端分离

image.png

Ajax发送请求

image.png

//1、新建XMLHttpRequest
const xhr = new XMLHttpRequest()

//2、监听状态改变
xhr.onreadystatechange = function(){
    console.log(xhr.response)
    //将字符串转成JSON
    const resJSON = JSON.parse(xhr.response)
}

//3、配置请求
//method:get post delete put...
xhr.open("get","url")

//4、发送请求
xhr.send()

XHR状态变化的监听

image.png 这个状态并非是HTTP的响应状态,而是记录的XMLHttpRequest对象的状态变化

XHR其他事件监听

image.png

XHR响应数据和响应类型

//1、新建XMLHttpRequest
const xhr = new XMLHttpRequest()

//2、监听状态改变
xhr.onreadystatechange = function(){
    console.log(xhr.resp)
    //将字符串转成JSON
    //const resJSON = JSON.parse(xhr.response) 有第三步的操作就不用转换了
}

//3、告知XHR请求的数据类型
xhr.responseType = 'json'

//4、发送请求
xhr.send()

HTTP的响应状态

image.png

xhr.status //状态码
xhr.statusText //获取状态描述

GET、POST请求传递参数

image.png

相关文章收集: juejin.cn/post/693829…