当前位置:   article > 正文

axios学习总结_axios失败返回非promise回调

axios失败返回非promise回调

目录

一、前言

二、axios 发送请求的方式(重要)

方式一:直接使用

 方式二:使用request方法 ,使用方式和直接使用一致

 方式三 、通过已经封装好的方法发送

 三、响应结果,服务端返回的数据

四、axios配置对象

  五、axios默认设置(重要)

 六、创建实例对象发送axios请求(重要)

七、拦截器(重要)

八、axios取消请求


一、前言
 

axios作用:用于web页面和服务器端数据的访问与传输。

二、axios 发送请求的方式(重要)

方式一:直接使用

  1. axios({
  2. //请求类型
  3. method: 'GET',
  4. //Url
  5. url: ' http://localhost:3000/posts/2',
  6. //发送给服务端的数据 key: "value" ,或者 "key" : "value" 单双引号都没问题
  7. data: {
  8. title:"今天天气好",
  9. author: "张三",
  10. }
  11. }).then(response=>{
  12. //回调 获取服务端返回的数据
  13. console.log(response);
  14. });
  15. then :自定义成功的回调函数;
  16. catch:自定义失败的回调函数

 方式二:使用request方法 ,使用方式和直接使用一致

  1. //请求
  2. but1[0].onclick=function(){
  3. axios.request({
  4. method:'POST',
  5. url: 'http://localhost:3000/posts',
  6. data:{
  7. title: "request",
  8. author: "发送数据"
  9. }
  10. }).then(response=>{
  11. console.log(response);
  12. })
  13. }
  14. //请求
  15. but1[0].onclick=function(){
  16. axios.request({
  17. method:'POST',
  18. url: 'http://localhost:3000/posts',
  19. data:{
  20. "title": "request",
  21. "author": "发送数据第二次"
  22. }
  23. }).then(response=>{
  24. console.log(response);
  25. })
  26. }

 方式三 、通过已经封装好的方法发送

  1. 1)、POST axios.post(url,JSON).then( lambda表达式)
  2. 以及其他的方法
  3. //发送POST请求
  4. but1[1].onclick=function(){
  5. //
  6. axios.post(
  7. 'http://localhost:3000/comments',
  8. {
  9. "body": "成功",
  10. "postId": 1
  11. }
  12. ).then(response=>{
  13. console.log(response);
  14. })
  15. }

 三、响应结果,服务端返回的数据

 then(response=>{});

        response从服务端得到的数据:

            config: 包含请求内容(请求类型、请求体、url等)。

            data: 响应体的结果, JSON对象 (服务器返回的结果数据)

            headers: 响应头信息

            request: 原生的AJAX请求对象

            status: 响应状态码

            statusText: 响应的状态字符串

四、axios配置对象

  1. url: '/user',
  2. method: 'get', //默认是get提交方法
  3. baseURL: '', //设置好之后可以直接发送请求路径,不用再写请求和协议
  4. transformRequest:[function(data,haeders){ //对请求的数据进行处理,处理完成后再发送给服务器
  5. return data;
  6. }],
  7. transformResponse: [function(data,haeders){ //可以对响应的结果进行改变
  8. return data;
  9. }],
  10. headers:{},// 对请求头信息进行配置
  11. params:{ //设置url参数
  12. ID:123
  13. },
  14. paramsSerializer:function(params){ //d对请求参数进行序列化转换成字符串
  15. }

  五、axios默认设置(重要)

  1. //默认设置
  2. axios.defaults.method='GET'; //设置默认请求
  3. axios.defaults.baseURL= 'http://localhost:3000'; //设置url请求前部分,url就可以只设置后部分
  4. axios.defaults.params={id:100};// 添加URL上的请求参数 ?id=100
  5. axios.defaults.timeout=3000; //超时时间 毫秒 时间过后请求超时,停止当前请求

示例:

  1. //默认设置
  2. axios.defaults.method='GET'; //设置默认请求
  3. axios.defaults.baseURL= 'http://localhost:3000'; //设置url请求前部分,url就可以只设置后部分
  4. axios.defaults.params={id:100};// 添加URL上的请求参数 ?id=100
  5. axios.defaults.timeout=3000; //超时时间 毫秒 时间过后请求超时,停止当前请求
  6. but1[0].onclick=function(){
  7. axios({
  8. method:'GET',
  9. url: '/posts',// baseURL 已经设置了默认的后部分
  10. }).then(response=>{
  11. console.log(response);
  12. })
  13. }

 六、创建实例对象发送axios请求(重要)

后端有多个放服务器进行数据传输时 用这种方式结构更加清除

 1、创建实例对象

通过axios.create() 方法创建

  1. const duanzi=axios.create({ //通过axios.create创建的对象duanzi同样具有axios的功能
  2. baseURL: 'https://api.apiopen.top', //设置默认url头部
  3. tiemout: 2000 //设置超时时间
  4. })

 2、    使用创建好的对象

这里duanzi 与axios对象功能几近是一样的

  1. //和axios 一样能直接使用
  2. duanzi(
  3. {
  4. url: '/getJoke'
  5. }
  6. ).then(respons=>{
  7. console.log(respons);
  8. })

  3、 创建的对象也能使用封装好的方法

  1. duanzi.get('/getJoke').then(response=>{
  2. console.log(response);
  3. })

七、拦截器(重要)

请求拦截器:在发送请求之前可以借助 函数来对请求参数和内容进行处理和检测,没问题直接发                         送,出现问题可以停止或取消请求的发送

 响应拦截器: 当服务器返回结果后,处理返回结果之前先对结果进行预处理或格式化处理.

  1. 发送请求 执行顺序 请求拦截器==>服务器==>响应拦截器==>自定义成功回调函数
  2. 正常顺序 : 请求拦截器成功回调==>响应拦截器成功回调==>自定义成功回调函数then
  3. 请求拦截器成功回调函数出错: 请求拦截器成功回调出错==>响应拦截器失败回调==>自定义失败回调函数catch

 请求拦截器

         request: axios.interceptors.request.use(成功回调函数,失败回调函数)

    响应拦截器

        response:  axios.interceptors.response.use(成功回调函数,失败回调函数)

  自定义回调函数:

         then :自定义成功的回调函数;

         catch:自定义失败的回调函数

示例:

  1. //和Promise有关
  2. //设置请求拦截器 自定义参数config 就是配置对象,可以对请求内容进行修改
  3. axios.interceptors.request.use(function (config){
  4. config.params={a:100};//在请求拦截器中对参数进行修改
  5. console.log(config);
  6. console.log("请求拦截器 成功");
  7. // throw '出错';
  8. return config;
  9. },function(error){
  10. console.log('请求拦截器 失败');
  11. return Promise.reject(error);
  12. } );
  13. //设置响应拦截器 自定义响应参数 response 只有响应体数据(服务器返回来的数据)
  14. axios.interceptors.response.use(function (response){
  15. console.log("响应拦截器 成功");
  16. return response;
  17. },function(error){
  18. console.log('响应拦截器 失败');
  19. return Promise.reject(error);
  20. } )
  21. axios({
  22. method:'GET',
  23. url: 'http://localhost:3000/posts'
  24. }).then(respons=>{
  25. console.log("自定义成功回调函数");
  26. console.log(respons);
  27. }).catch(reason=>{
  28. console.log("自定义失败回调函数");
  29. });

八、axios取消请求

通过cancelToken 创建一个可取消的函数 axios.CancelToken

  1. var cancel=null; //创捷的取消请求函数,也可以用来判断当前请求是否执行完 未执行完就为 取消请求方法,执行完后就在then赋值为null
  2. //按下发送当前请求
  3. but1[4].onclick=function(){
  4. axios({
  5. method:'GET',
  6. url:'http://localhost:3000/posts',
  7. //1.添加配置对象的属性
  8. cancelToken: new axios.CancelToken(function(c){
  9. //将c的值赋值给cancel, cancel() 方法就具有了取消当前请求的能力
  10. cancel=c;
  11. })
  12. }).then(respo=>{
  13. console.log(respo);
  14. cancel=null;//赋值为null,表示请求已经执行完,当前请求过程不再需要 取消请求方法
  15. })
  16. }
  17. //按下按钮取消对应的请求的发送
  18. but1[5].onclick=function(){
  19. if(cancel!==null){ //请求未执行完才能取消请求
  20. cancel(); //直接调用cancel()方法取消请求
  21. }
  22. }

仅供学习参考,有错误还请在评论区指出

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57101
推荐阅读
相关标签
  

闽ICP备14008679号