• AJAX——封装_简易axios


    1.简易axios_获取身份列表

    需求:基于Promise + XHR 封装 myAxios函数,获取省份列表展示

    步骤:

    1.定义 myAxios函数,接收配置对象,返回Promise对象

    2.发起XHR请求,默认请求方法为GET

    3.调用成功/失败的处理程序

    4.使用myAxios函数,获取省份列表展示

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>封装_简易axios函数_获取省份列表</title>
    8. </head>
    9. <body>
    10. <p class="my-p"></p>
    11. <script>
    12. /**
    13. * 目标:封装_简易axios函数_获取省份列表
    14. * 1. 定义myAxios函数,接收配置对象,返回Promise对象
    15. * 2. 发起XHR请求,默认请求方法为GET
    16. * 3. 调用成功/失败的处理程序
    17. * 4. 使用myAxios函数,获取省份列表展示
    18. */
    19. // 1. 定义myAxios函数,接收配置对象,返回Promise对象
    20. function myAxios(config){
    21. return new Promise((resolve, reject) => {
    22. //2.发起XHR请求,默认请求方法GET
    23. const xhr = new XMLHttpRequest()
    24. xhr.open(config.method || 'GET', config.url)
    25. xhr.addEventListener('loadend', () => {
    26. //3.调用成功/失败的处理程序
    27. if (xhr.status >= 200 && xhr.status < 300){
    28. resolve(JSON.parse(xhr.response))
    29. } else {
    30. reject(new Error(xhr.response))
    31. }
    32. })
    33. xhr.send()
    34. })
    35. }
    36. // 4. 使用myAxios函数,获取省份列表展示
    37. myAxios({
    38. url:'http://hmajax.itheima.net/api/province'
    39. }).then(result => {
    40. console.log(result)
    41. document.querySelector('.my-p').innerHTML = result.list.join('
      '
      )
    42. }).catch(error => {
    43. console.log(error)
    44. document.querySelector('.my-p').innerHTML = error.message
    45. })
    46. </script>
    47. </body>
    48. </html>

    2.简易axios_获取地区列表

    需求:修改myAxios函数支持传递查询参数,获取“辽宁省”,“大连市”对应地区列表展示

    要求:

    1. myAxios函数调用后,传入params选项
    2. 基于URLSearchParams转换查询参数字符串

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>封装_简易axios函数_获取地区列表</title>
    8. </head>
    9. <body>
    10. <p class="my-p"></p>
    11. <script>
    12. /**
    13. * 目标:封装_简易axios函数_获取地区列表
    14. * 1. 判断有params选项,携带查询参数
    15. * 2. 使用URLSearchParams转换,并携带到url上
    16. * 3. 使用myAxios函数,获取地区列表
    17. */
    18. function myAxios(config) {
    19. return new Promise((resolve, reject) => {
    20. const xhr = new XMLHttpRequest()
    21. // 1. 判断有params选项,携带查询参数
    22. if(config.params){
    23. //2.使用URLSearchParams转换,并携带到url上
    24. const paramsObj = new URLSearchParams(config.params)
    25. const queryString = paramsObj.toString()
    26. // 把查询参数字符串,拼接在url?后面
    27. config.url += `?${queryString}`
    28. }
    29. xhr.open(config.method || 'GET', config.url)
    30. xhr.addEventListener('loadend', () => {
    31. if (xhr.status >= 200 && xhr.status < 300) {
    32. resolve(JSON.parse(xhr.response))
    33. } else {
    34. reject(new Error(xhr.response))
    35. }
    36. })
    37. xhr.send()
    38. })
    39. }
    40. // 3. 使用myAxios函数,获取地区列表
    41. myAxios({
    42. url:'http://hmajax.itheima.net/api/area',
    43. params: {
    44. pname:'辽宁省',
    45. cname:'大连市'
    46. }
    47. }).then(result => {
    48. console.log(result)
    49. document.querySelector('.my-p').innerHTML = result.list.join('
      '
      )
    50. })
    51. </script>
    52. </body>
    53. </html>

    3.简易axios_注册用户

    需求:修改myAxios函数支持传递请求头数据,完成注册用户功能

    步骤:

    1. myAxios函数调用后,判断data选项
    2. 转换数据类型,在send方法中发送
    3. 使用自己封装的myAxios函数完成注册用户功能
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>封装_简易axios函数_注册用户</title>
    8. </head>
    9. <body>
    10. <button class="reg-btn">注册用户</button>
    11. <script>
    12. /**
    13. * 目标:封装_简易axios函数_注册用户
    14. * 1. 判断有data选项,携带请求体
    15. * 2. 转换数据类型,在send中发送
    16. * 3. 使用myAxios函数,完成注册用户
    17. */
    18. function myAxios(config) {
    19. return new Promise((resolve, reject) => {
    20. const xhr = new XMLHttpRequest()
    21. if (config.params) {
    22. const paramsObj = new URLSearchParams(config.params)
    23. const queryString = paramsObj.toString()
    24. config.url += `?${queryString}`
    25. }
    26. xhr.open(config.method || 'GET', config.url)
    27. xhr.addEventListener('loadend', () => {
    28. if (xhr.status >= 200 && xhr.status < 300) {
    29. resolve(JSON.parse(xhr.response))
    30. } else {
    31. reject(new Error(xhr.response))
    32. }
    33. })
    34. // 1. 判断有data选项,携带请求体
    35. if (config.data) {
    36. //2. 转换数据类型,在send中发送
    37. const jsonStr = JSON.stringify(config.data)
    38. xhr.setRequestHeader('Content-Type','application/json')
    39. xhr.send(jsonStr)
    40. } else {
    41. // 如果没有请求体数据,正常的发起请求
    42. xhr.send()
    43. }
    44. })
    45. }
    46. document.querySelector('.reg-btn').addEventListener('click',() => {
    47. // 3. 使用myAxios函数,完成注册用户
    48. myAxios({
    49. url:'http://hmajax.itheima.net/api/register',
    50. method:'POST',
    51. data:{
    52. username:'itheima999',
    53. password:'666666'
    54. }
    55. }).then(result => {
    56. console.log(result)
    57. }).catch(error => {
    58. console.dir(error)
    59. })
    60. })
    61. </script>
    62. </body>
    63. </html>

  • 相关阅读:
    关于一篇“范式详解”博文的批注
    Android 11 设置开机默认系统横屏显示
    自认为最好的rule_of_five
    论如何在Android中还原设计稿中的阴影
    [附源码]java毕业设计教务系统
    Cascade-MVSNet论文笔记
    【小月电子】ALTERA FPGA开发板(Spirit_V4)系统学习教程-LESSON3 LED流水灯
    HCIP路由交换的三门考试
    如何判断去噪之后的变压器局部放电是内部还是外部信号
    vue3父子组件传对象,子组件访问修改父组件对象中的属性值
  • 原文地址:https://blog.csdn.net/weixin_48719464/article/details/138054286