• axios (get,post,put,delete),常用配置,全局配置,axios.create(config)配置一个新的axios


    目录

    axios 的理解和应用

    axios 特点:

    使用 axios 发 ajax请求

    1. 发送 GET 请求

    axios get完整版:

    axios get精简版:

    axios get请求发送时携带query参数:

    axios get携带 query参数完整版:

    axios get携带 query参数精简版:

    发送 POST 请求:

    axios post完整版:

    axios post精简版:

    发送 PUT 请求:

    axios put完整版:

    axios put精简版:

    发送 DELETE 请求:

    axios DELETE  代码:

    axios 常用配置:

    axios 可以全局配置:

    axios.create(config)配置一个新的axios


    axios 的理解和应用

    axios 是什么?
    前端最流行的 ajax 的请求库
    React/Vue 官方都推荐使用 axios 发 ajax 请求
    文档:http://github.com/axios/axios


    axios 特点:

    1. 基本 promise 的异步 ajax 请求库
    2. 浏览器 / node 端都可以使用
    3. 支持请求 / 响应拦截器
    4. 支持请求取消
    5. 请求 / 响应数据转换
    6. 批量发送多个请求

    使用 axios 发 ajax请求

    1. axios 调用的返回值是 promise 实例
    2. 成功的值叫 response ,失败的值叫 error
    3. axios 成功的值是一个 axios 封装的 response 对象,服务器返回的真正数据在 response.data 中

    1. 发送 GET 请求

    1. <body>
    2. <button id="btn">点我获取所有人的信息</button>
    3. <script>
    4. //获取按钮
    5. const btn = document.getElementById('btn');
    6. //获取所有人的信息 发送GET请求 --- 不携带参数
    7. btn.onclick = () => {
    8. //此处写axios代码!
    9. }
    10. </script>

    axios get完整版:

    1. const result = axios({
    2. url: 'http://localhost:5000/persons',//请求地址
    3. method: 'GET',//请求方式
    4. });
    5. result.then(
    6. response => {console.log('请求成功了!', response.data);},
    7. error => {console.log('请求失败了,', error);}
    8. );

    axios get精简版:

    1. axios.get('http://localhost:5000/persons').then(
    2. response => { console.log('请求成功了!', response.data); },
    3. error => { console.log('请求失败了,', error); }
    4. );

    axios get请求发送时携带query参数:

    1. <body>
    2. <button id="btn1">点我获取所有人的信息</button><br><br>
    3. <button id="btn2">点我获取某个人的信息</button>
    4. <input type="text" id="person_id" placeholder="请输入id">
    5. <!--
    6. 1. axios 调用的返回值是 promise 实例
    7. 2.成功的值叫 response,失败的值叫 error
    8. 3.axios成功的值是一个axios封装的response对象,服务器返回的真正数据在response.data
    9. 4.携带query参数时,编写的配置项叫做params
    10. -->
    11. <script>
    12. //获取按钮
    13. const btn1 = document.getElementById('btn1');
    14. const btn2 = document.getElementById('btn2');
    15. const personId = document.getElementById('person_id');
    16. //获取所有人的信息 发送GET请求 --- 不携带参数
    17. btn1.onclick = () => {
    18. ...........
    19. }
    20. //获取某个人的信息 发送get请求 --- 携带query参数
    21. btn2.onclick = () => {
    22. //此处写入axios代码
    23. }
    24. </script>
    25. </body>

    axios get携带 query参数完整版:

    1. axios({
    2. url:'http://localhost:5000/person',
    3. method:'GET',
    4. params:{id:personId.value}//此处写的params但其实传的是query参数
    5. }).then(
    6. response =>{console.log('成功了',response.data);},
    7. error =>{console.log('失败了',error);}
    8. )

    axios get携带 query参数精简版:

    1. axios.get('http://localhost:5000/person',{params:{id:personId.value}},).then(
    2. response =>{console.log('成功了',response.data);},
    3. error =>{console.log('失败了',error);}
    4. )

    发送 POST 请求:

    1. <body>
    2. <button id="btn1">点我获取所有人的信息</button><br><br>
    3. <button id="btn2">点我获取某个人的信息</button>
    4. <input type="text" id="person_id" placeholder="请输入id">
    5. <button id="btn3">点我添加一个人</button>
    6. <input type="text" id="person_age" placeholder="请输入age">
    7. <input type="text" id="person_name" placeholder="请输入name"><br><br>
    8. <script>
    9. //获取按钮
    10. const btn1 = document.getElementById('btn1');
    11. const btn2 = document.getElementById('btn2');
    12. const btn4 = document.getElementById('btn4');
    13. const personId = document.getElementById('person_id');
    14. const personAge = document.getElementById('person_age');
    15. const personName = document.getElementById('person_name');
    16. //获取所有人的信息 发送GET请求 --- 不携带参数
    17. btn1.onclick = () => {
    18. ...........
    19. }
    20. //获取某个人的信息 发送get请求 --- 携带query参数
    21. btn2.onclick = () => {
    22. ...........
    23. }
    24. //添加某个人 发送post请求 --- 携带请求体body参数
    25. btn3.onclick = () =>{
    26. //此处写入axios代码
    27. }
    28. </script>
    29. </body>

    axios post完整版:

    1. axios({
    2. url:'http://localhost:5000/person',
    3. method:'POST',
    4. // data:{name:personName.value,age:personAge.value},//携带请求体参数(默认json编码)
    5. data:`name=${personName.value}&age=${personAge.value}`,//携带请求体参数(urlencoded编码)
    6. }).then(
    7. response =>{console.log('成功了'+response.data);},
    8. error =>{console.log('失败了'+error);}
    9. )

    axios post精简版:

    1. //使用对象就会默认是json编码模式
    2. //axios.post('http://localhost:5000/person',{name:personName.value,age:personAge.value}).then(
    3. //是用字符串就会默认是urlencoded编码模式
    4. axios.post('http://localhost:5000/person',`name=${personName.value}&age=${personAge.value}`).then(
    5. response =>{console.log('成功了'+response.data);},
    6. error =>{console.log('失败了'+error);}
    7. )

    备注:axios 底层源码中也是写入一个判断,假如第二个空中为对象则使用 JSON.stringify 来解码,假如第二个空为模板字符串则使用urlencoded方式来解码。

    发送 PUT 请求:

    1. <body>
    2. <button id="btn1">点我获取所有人的信息</button><br><br>
    3. <button id="btn2">点我获取某个人的信息</button>
    4. <input type="text" id="person_id" placeholder="请输入id"><br><br>
    5. <button id="btn3">点我添加一个人</button>
    6. <input type="text" id="person_age" placeholder="请输入age">
    7. <input type="text" id="person_name" placeholder="请输入name"><br><br>
    8. <button id="btn4">点我更新一个人</button>
    9. <input type="text" id="person_update_id" placeholder="请输入一个人的id">
    10. <input type="text" id="person_update_age" placeholder="请输入一个人的年龄">
    11. <input type="text" id="person_update_name" placeholder="请输入一个人的姓名">
    12. <script>
    13. //获取按钮
    14. const btn1 = document.getElementById('btn1');
    15. const btn2 = document.getElementById('btn2');
    16. const btn3 = document.getElementById('btn3');
    17. const btn4 = document.getElementById('btn4');
    18. const personId = document.getElementById('person_id');
    19. const personAge = document.getElementById('person_age');
    20. const personName = document.getElementById('person_name');
    21. const personUpdateId = document.getElementById('person_update_id');
    22. const personUpdateAge = document.getElementById('person_update_age');
    23. const personUpdateName = document.getElementById('person_update_name');
    24. //获取所有人的信息 发送GET请求 --- 不携带参数
    25. btn1.onclick = () => {
    26. ........
    27. }
    28. //获取某个人的信息 发送get请求 --- 携带query参数
    29. btn2.onclick = () => {
    30. ........
    31. }
    32. //添加某个人 发送post请求 --- 携带请求体body参数
    33. btn3.onclick = () => {
    34. ........
    35. }
    36. //更新一个人 发送PUT请求--携带json编码参数 或 urlencoded编码
    37. btn4.onclick = () => {
    38. //此处写入axios代码
    39. }
    40. </script>
    41. </body>

    axios put完整版:

    1. //完整版
    2. axios({
    3. url: 'http://localhost:5000/person',
    4. method: 'PUT',
    5. data:{
    6. id:personUpdateId.value,
    7. name:personUpdateName.value,
    8. age:personAge.value
    9. }
    10. }).then(
    11. response => { console.log('成功了' + response.data); },
    12. error => { console.log('失败了' + error); }
    13. )

    axios put精简版:

    1. //精简版
    2. axios.put("http://localhost:5000/person",{
    3. id:personUpdateId.value,
    4. name:personUpdateName.value,
    5. age:personAge.value,
    6. }).then(
    7. response => { console.log('成功了' + response.data); },
    8. error => { console.log('失败了' + error); }
    9. )

    发送 DELETE 请求:

    1. <body>
    2. <button id="btn1">点我获取所有人的信息</button><br><br>
    3. <button id="btn2">点我获取某个人的信息</button>
    4. <input type="text" id="person_id" placeholder="请输入id"><br><br>
    5. <button id="btn3">点我添加一个人</button>
    6. <input type="text" id="person_age" placeholder="请输入age">
    7. <input type="text" id="person_name" placeholder="请输入name"><br><br>
    8. <button id="btn4">点我更新一个人</button>
    9. <input type="text" id="person_update_id" placeholder="请输入一个人的id">
    10. <input type="text" id="person_update_age" placeholder="请输入一个人的年龄">
    11. <input type="text" id="person_update_name" placeholder="请输入一个人的姓名"><br><br>
    12. <button id="btn5">点我删除一个人</button>
    13. <input type="text" id="person_delete_id" placeholder="请输入一个人的id">
    14. <!--
    15. 1. axios 调用的返回值是 promise 实例
    16. 2.成功的值叫 response,失败的值叫 error
    17. 3.axios成功的值是一个axios封装的response对象,服务器返回的真正数据在response.data
    18. 4.携带query参数时,编写的配置项叫做params
    19. 5.携带params参数时,就需要自己手动拼在URL中
    20. -->
    21. <script>
    22. //获取按钮
    23. const btn1 = document.getElementById('btn1');
    24. const btn2 = document.getElementById('btn2');
    25. const btn3 = document.getElementById('btn3');
    26. const btn4 = document.getElementById('btn4');
    27. const btn5 = document.getElementById('btn5');
    28. const personId = document.getElementById('person_id');
    29. const personAge = document.getElementById('person_age');
    30. const personName = document.getElementById('person_name');
    31. const personUpdateId = document.getElementById('person_update_id');
    32. const personUpdateAge = document.getElementById('person_update_age');
    33. const personUpdateName = document.getElementById('person_update_name');
    34. const personDeleteId = document.getElementById('person_delete_id');
    35. //获取所有人的信息 发送GET请求 --- 不携带参数
    36. btn1.onclick = () => {
    37. .........
    38. }
    39. //获取某个人的信息 发送get请求 --- 携带query参数
    40. btn2.onclick = () => {
    41. .........
    42. }
    43. //添加某个人 发送post请求 --- 携带请求体body参数
    44. btn3.onclick = () => {
    45. ........
    46. }
    47. //更新一个人 发送PUT请求--携带json编码参数 或 urlencoded编码
    48. btn4.onclick = () => {
    49. .........
    50. }
    51. //删除一个人 发送delete请求---携带params参数
    52. btn5.onclick = () => {
    53. //此处写入axios代码
    54. }
    55. </script>
    56. </body>

    axios DELETE  代码:

    1. //使用的params参数的方式
    2. axios({
    3. url:`http://localhost:5000/person/${personDeleteId.value}`,
    4. method:'DELETE',
    5. }).then(
    6. response =>{console.log('成功了',response.data);},
    7. error =>{console.log('失败了',error);}
    8. )

    axios 常用配置:

    1. axios({
    2. url:"http://localhost:5000/test1",//请求地址
    3. method:'GET',//请求方式
    4. params:{delay:3000,b:2},//配置query参数
    5. data:{c:3,d:4},//配置请求体参数(json编码)
    6. data:`e=5&f=6`,//配置请求体参数(urlencoded编码)
    7. timeout:2000,//配置超市时间
    8. Headers:{demo:123},//配置请求头
    9. responseType: 'json'//配置响应数据的格式(默认是json)
    10. })

    axios 可以全局配置:

    1. //给axios配置默认属性
    2. axios.defaults.timeout= 2000;//配置了默认超时时间
    3. axios.defaults.headers={school:'金科'};//配置每个请求的请求头都自带的内容
    4. axios.defaults.baseURL='http://localhost:5000';
    5. //配置请求的地址,若不配置,则axios默认从自身的地址发送请求;若配置了,写请求时不需要带以上写过的地址,只需要写后面的地址会自动拼接!

    axios.create(config)配置一个新的axios

    1. 根据指定配置创建一个新的 axios ,也就是每个新 axios 都自己的配置
    2. 新的 axios 只是没有取消请求和批量发请求的方法,其他所有的语法都是一致的,所以不是100%一样
    3. 为什么要这个语法?

    ​ 需要:项目中有部分接口需要的配置与另一部分接口需要的配置不一样

    1. <body>
    2. <button id="btn1">点我获取所有人</button>
    3. <button id="btn3">点我获取笑话信息</button>
    4. <script>
    5. const btn1 = document.getElementById('btn1');
    6. const btn3 = document.getElementById('btn3');
    7. //配置axios.create的内容
    8. const axios2 = axios.create({
    9. baseURL:'https//api.apiopen.top',
    10. timeout:3000,
    11. // headers:{name:'王成'}
    12. })
    13. //给axios配置默认属性
    14. axios.defaults.timeout = 2000;
    15. axios.defaults.headers = { school: '金科' };
    16. axios.defaults.baseURL = 'http://localhost:5000';
    17. btn1.onclick = () => {
    18. axios({
    19. ..........
    20. }
    21. btn3.onclick = () => {
    22. axios2({
    23. url:'/getJoke',
    24. method:'GET',
    25. }).then(
    26. response =>{console.log('成功了',response.data);},
    27. error =>{console.log('失败了',error);}
    28. )
    29. }
    30. </script>
    31. </body>

    备注:axios.create( ) 配置 需要写在 axios 全局配置的前面,否则会报错(目前版本0.27.2)之前某个版本可用

  • 相关阅读:
    计算机毕业设计Java百姓点评网的设计与实现(源码+系统+mysql数据库+lw文档)
    vue3【计算属性与监听-详】
    JavaScript(五):优雅的async/await
    【LeetCode:2216. 美化数组的最少删除数 | 贪心】
    【教程7】疯壳·ARM功能手机-BLE透传实验教程
    算法基础(二)| 高精度算法详解
    CSS变量的定义和使用 var(变量)
    基于高斯混合模型的分布拟合与参数估计:高维复数域的详细推导
    webpack 压缩图片
    【Android -- 开源库】fastjson 基本使用
  • 原文地址:https://blog.csdn.net/zqlbanzhuan/article/details/128132133