• Ajax用法总结


    目录

    Ajax简介

    Ajax使用

    xhr内部五种状态

    Ajax在IE浏览器上存在的缓存问题

    如何发送post请求

    如何取消Ajax请求

    jQuery封装的Ajax如何使用?


    Ajax简介

    Ajax全称为Asynchous Javascript And XML,即异步的JS和XML,通过Ajax可以在浏览器中向服务器发送异步请求,其最大的特点是实现:

    页面无刷新获取数据

    工作原理其实相当于在用户和服务器之间加上了一个中间层(Ajax引擎),使用户操作与服务器响应异步化;

    它不是一个新的编程语言,而是一种将现有标准组合在一起使用的技术方案,但并不是一种新技术;它依赖的是现有的CSS/HTML/Javascript,而其中最核心的依赖是浏览器提供的XMLHttpRequest对象,是这个对象使得浏览器可以发出HTTP请求与接收HTTP响应。

    优点:

    • Ajax无需刷新页面即可与服务器端进行通信
    • 可允许用户根据用户事件来更新部分页面内容

    缺点:

    • 并没有浏览历史,不能回退(因为无刷新页面)
    • 存在跨域问题
    • SEO不友好

    补充:

    此处XML指可扩展标记语言,它和HTML类似,不同的是HTML中都是预定义标签,而XML中没有预定义标签,全都是自定义标签,用于表示一些数据(后逐步被json替代,但微信公众号等应用中还会用到)

    1. // 用XML表示person数据
    2. <person>
    3. <name>小明name>
    4. <age>12age>
    5. person>

    Ajax使用

    利用XMLHttpRequest(以下简称xhr)发送Ajax请求,各个功能说明如下:

    const xhr = new XMLHttpRequest()      // 创建XMLHttpRequest实例对象

    xhr.open(method, url)    // 配置请求

    xhr.setRequestHeader(key, value)      // 设置请求头(可选)

    xhr.timeout  = 2000 //  请求超时时间

    xhr.send()    //  发送请求, get请求不传body参数

    xhr.onreadystatechange = () => {

            if (xhr.readyState === 4 && xhr.status === 200 ) {

                    // 对响应的处理

            }

    }  //  接收响应

    xhr.onerror = ( )  => {

            // 对请求出错的处理

    }

    xhr.ontimeout = () => {

           // 对超时出错的处理

    }

    xhr.abort() // 取消请求

     示例:get携带params参数id请求name属性:

    1. <script>
    2. const xhr = new XMLHttpRequest() // 创建XMLHttpRequest实例对象
    3. xhr.open('GET', 'http://localhost:9123/students/:id') // 携带params参数
    4. xhr.send()
    5. xhr.onreadystatechange = () => {
    6. if (xhr.readyState === 4 && xhr.status === 200 ) {
    7. const span = document.querySelector('span')
    8. span.innerHTML = xhr.response // 将接口返回数据放到span标签中
    9. }
    10. } //  接收响应
    11. script>

    xhr内部五种状态

    xhr内部有五种状态,分别是0/1/2/3/4,经过了四次状态转变;

    0:在xhr实例对new出来的那一刻状态即为0(初始状态);

    1:在open()调用之后,状态变为1,此时可以修改请求头内容;

    2:在send()调用之后,此处调用请求头报错;

    3:已经返回一部分简单数据和响应头;

    4:所有数据都已经返回回来;

    Ajax在IE浏览器上存在的缓存问题

    问题描述:在IE浏览器中,由于缓存机制的存在,A若发送相同的ajax get请求时(post请求无此问题),只会将第一次请求发送出去,剩余的多次会直接使用缓存中的数据;

    如多次请求get接口http://localhost:8080/get_name,ie浏览器会默认将第一次得到的数据直接返回过去,不再请求服务器,但是如果该接口返回的数据在别处进行了修改后 ,即发生了改变后,此时再用缓存中的数据就不对了,所以要让ie浏览器在每次调用相同接口时,都要重新请求服务器,解决方法就是在接口地址上加上时间戳参数:'http://localhost:8080/get_name?t=' + Date.now()`

    如何发送post请求

    众所周知,发送post请求有三种携带参数方式,query、params以及请求体方式,其中请求体方式有两种参数方式----urlencoded以及json方式,这部分内容介绍可参见文章后面补充;一般情况下,使用post方式配合请求体传参;

    在Ajax中,若要用post方式,则要声明对应的请求头参数:

    1. // 二选一
    2. xhr.setRequestHeader('Content-type', 'application/json') // json方式传参
    3. xhr.setRequestHeader('Content-type', 'application/x-www-form-rulencoded') // urlencoded方式传参

    同时要在xhr.send()配置要传输的参数,要与请求头设置的方式对应;

    1. // json方式
    2. const person = {name: '小明', age: 12}
    3. xhr.send(JSON.stringify(person))
    4. // urlencoded方式
    5. xhr.send('name=小明&age=12')

    完整示例:post方式请求http://localhost:9123/students下的数据,并携带请求体参数name和age

    1. <span>span>
    2. <script>
    3. const xhr = new XMLHttpRequest() // 创建XMLHttpRequest实例对象
    4. xhr.open('POST', 'http://localhost:9123/students')
    5. xhr.responseType = 'json'
    6. const person = {name: '小明', age: 12}
    7. xhr.setRequestHeader('Content-type', 'application/json') // json方式传参
    8. // xhr.setRequestHeader('Content-type', 'application/x-www-form-rulencoded') // urlencoded方式传参
    9. xhr.onreadystatechange = () => {
    10. if (xhr.readyState === 4 && xhr.status === 200 ) {
    11. const span = document.querySelector('span')
    12. span.innerHTML = xhr.response
    13. }
    14. } //  接收响应
    15. xhr.send(person) // json格式
    16. // xhr.send('name=小明&age=12') // urlencoded
    17. script>

    如何取消Ajax请求

    所谓取消,是指在发出Ajax请求后,通过用户交互(如点击取消按钮)取消刚刚发送的Ajax请求;

    使用xhr.abort()即可;

    如下示例展示如何在点击多次请求后,只保留最后一次接口请求:

    1. <span>span>
    2. <button id="btn">发送button>
    3. <script>
    4. let xhr
    5. let isLoading
    6. const btn = document.querySelector('#btn')
    7. btn.onclick = () => {
    8. if (isLoading) {
    9. // 如果不是最后一次则取消
    10. xhr.abort()
    11. }
    12. xhr = new XMLHttpRequest() // 创建XMLHttpRequest实例对象
    13. xhr.onreadystatechange = () => {
    14. if (xhr.readyState === 4 && xhr.status === 200) {
    15. // 若结果还未返回回来,则此处一直未执行
    16. isLoading = false
    17. const span = document.querySelector('span')
    18. span.innerHTML = xhr.response
    19. }
    20. } //  接收响应
    21. xhr.open('GET', 'http://localhost:9123/students/1')
    22. xhr.responseText = 'json'
    23. xhr.send()
    24. isLoading = true
    25. }
    26. script>

    jQuery封装的Ajax如何使用?

    jQuery对Ajax进行了封装,使得原生xhr方式更加简单,仅供了解:

    代码中引入jQuery库后,全局中就会出现$和jQuery变量;

    发送post请求简单形式:

    $.post(url, [data], [callback], [type])

    url: 请求的URL地址;

    data: 请求携带的参数;

    callback: 请求成功时的回调函数;

    type: 设置请求返回的内容格式,如json、html、xml等

    发送get请求简单形式:

     $.get(url, [data], [callback], [type])   参数含义与上相同

    发送完整请求例子: 

    1. <button id="btn1">getbutton>
    2. <button id="btn2">postbutton>
    3. <span id="span">span>
    4. <script>
    5. const btn1 = $('#btn1')
    6. const btn2 = $('#btn2')
    7. const span = $('#span')
    8. btn1.click(() => {
    9. $.ajax({
    10. url: 'http://localhost:9123/students/1', // 请求地址
    11. method: 'GET', // 请求方式
    12. // data: { id: '1' },
    13. dataType: 'json',
    14. timeout: 2000,
    15. success: (res, err, xhr) => {
    16. console.log(res);
    17. span.html(JSON.stringify(res))
    18. },
    19. err: (xhr) => {
    20. console.log('失败了', xhr);
    21. }
    22. })
    23. })
    24. btn2.click(() => {
    25. $.ajax({
    26. url: 'http://localhost:9123/students', // 请求地址
    27. method: 'POST', // 请求方式
    28. data: { id: '1' , name: "apple"}, // 请求体参数
    29. dataType: 'json',
    30. timeout: 2000,
    31. success: (res, err, xhr) => {
    32. console.log(res);
    33. span.html(JSON.stringify(res))
    34. },
    35. err: (xhr) => {
    36. console.log('失败了', xhr);
    37. }
    38. })
    39. })
    40. script>

    jQuery默认传的请求体形式为urlencoded形式

    上面post请求的简单版示例为:

    1. btn2.click(() => {
    2. $.post('http://localhost:9123/students', {id: 1}, (res)=> {
    3. console.log(res);
    4. span.html(JSON.stringify(res))
    5. })
    6. })

    补充:fetch是什么?

     fetch也是浏览器的内置函数/方法,在级别上与ajax是一样的,feth支持promise、await、async,它更加符合关注分离(Separation of Concerns)原则;官网介绍在fetch documentation

     简单使用方式如下:

    1. fetch(url, options).then(function(response) {
    2. // handle HTTP response
    3. // 这一步主要是测试是否能与服务器建立连接,可以建立连接走response
    4. }, function(error) {
    5. // handle network error
    6. // 建立连接失败走error
    7. })

     添加配置项方式如下:

    1. fetch(url, {
    2. method: "POST",
    3. body: JSON.stringify(data),
    4. headers: {
    5. "Content-Type": "application/json"
    6. },
    7. credentials: "same-origin"
    8. }).then(function(response) {
    9. response.status //=> number 100–599
    10. response.statusText //=> String
    11. response.headers //=> Headers
    12. response.url //=> String
    13. return response.text() // 这里返回出去的才是连接成功拿到的数据,返回的是一个promise,见下方说明
    14. }, function(error) {
    15. error.message //=> String
    16. })

     

    如下是配合await与async方法示例:

    1. // index.jsx
    2. export default class todoList extends React.Component {
    3. test = async() => {
    4. try {
    5. const response = await fetch('/api/test', {method: 'get'})
    6. const data = await response.text()
    7. console.log(data, 'data是啥') // 数据返回过去了
    8. } catch (err) {
    9. console.log(err)
    10. }
    11. }
    12. render() {
    13. return (
    14. <button onClick={this.test}>testbutton>
    15. )
    16. }
    17. }
    1. // app.js
    2. app.get('/test', (req, res) => {
    3. console.log('请求拿到没')
    4. res.send('数据返回过去了')
    5. })

  • 相关阅读:
    AI全栈大模型工程师(二)课程大纲
    java基于springboot的网上商城购物系统
    力扣 1668. 最大重复子字符串
    【MySQL】-增删查改
    Stream流中的groupingBy分组操作
    window10环境构建和运行skia源码
    【Redis】Redis在Linux与windows上的安装&基本操作语法
    Vue.js源码解析:Vue.js 常用工具函数 util.js
    好的架构是进化来的,不是设计来的
    腾讯云16核服务器配置大全_CVM和轻量服务器汇总
  • 原文地址:https://blog.csdn.net/ks795820/article/details/128166402