>>分享Web前端开发技术,并对孙卫琴的《精通Vue.js:Web前端开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 15874 个阅读者 刷新本主题
 * 贴子主题:  vue中axios异步调用接口的坑 回复文章 点赞(0)  收藏  
作者:Jacky    发表时间:2021-02-04 06:00:43     消息  查看  搜索  好友  邮件  复制  引用

背景

最近在写vue项目的时候遇到一个axios调用接口的坑,有个前端模块涉及axios去调用多个接口,然后请求完获取数据之后,再使用windows.location.href重定向到新页面,这时候却发现axios请求的接口都是出于canceled的状态。

例如:

axios.get('/url1')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
axios.get('/url2')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
   windows.location.href="/

如果是这么写的话,由于axios调用接口是异步的,极有可能在url1和url2还没请求完毕的时候,windows.location.href就被执行了。这时在当前页面打开一个新的页面,而通过chrome的f12控制台查看url1和url2请求都是处于 canceled状态。

StackOverflow网上查的canceled解释如下:

1.The DOM element that caused the request to be made got deleted (i.e. an IMG is being loaded, but before the load happened, you deleted the IMG node)

2.You did something that made loading the data unnecessary. (i.e. you started loading a iframe, then changed the src or overwrite the contents)

3.There are lots of requests going to the same server, and a network problem on earlier requests showed that subsequent requests weren’t going to work (DNS lookup error, earlier (same) request resulted e.g. HTTP 400 error code, etc)

简单来说,就是元素或者请求还未完成的时候,被打断了。

解决方法

这里有两个方法:第一个就是直接把windows.location.href包裹在axios请求完成的处理流程里。如下面:

axios.get('/url1')
  .then(function (response) {
    axios.get('/url2')
      .then(function (response) {        windows.location.href="/"
      })
    .catch(function (error) {
        console.log(error);
    });
  })
  .catch(function (error) {
    console.log(error);
  });

这么做的好处就是只有等到url1和url2都请求成功后,页面才会跳转。但是有个新的问题,如果要多次请求url1和url2之后再进行跳转该怎么写?例如:

for    //(循环)
{
axios.get('/url1')
      .then(function (response) {
        axios.get('/url2')
          .then(function (response) {
            
          })
        .catch(function (error) {
            console.log(error);
        });
      })
.catch(function (error) {
        console.log(error);
   });
}windows.location.href="/"

如果是这样的话,axios请求可能还没完成就跳转了。axios是异步的,所以windows.location.href有可能先于axios执行完。

在这里有个取巧的方法,使用定时器来延时windows.location.href的执行时机。

setTimeout(function() {
    windows.location.href="/"
}, 2000);

  这样写,基本可以保证windows.location.href在axios之后执行(取决于axios调用接口的和业务逻辑处理的时间)
  博主:测试生财

座右铭:专注测试与自动化,致力提高研发效能;通过测试精进完成原始积累,通过读书理财奔向财务自由。

csdn:https://blog.csdn.net/ccgshigao

博客园:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374
         ----------------------------
原文链接:https://blog.51cto.com/14900374/2562751

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2021-02-14 18:44:56 重新编辑]
  Java面向对象编程-->集合(下)
  JavaWeb开发-->Web运作原理(Ⅲ)
  JSP与Hibernate开发-->数据库事务的并发问题的解决方案
  Java网络编程-->安全网络通信
  精通Spring-->Vue简介
  Vue3开发-->绑定表单
  vue3.0-基本特性和用法
  Vue3.0里为什么要用 Proxy API 替代 defineProperty API ? ...
  web前端工程师面试题的知识考点盘总
  javaScript中==和===的区别详解
  Vue选项的用法
  Vue CLI内网安装(禁止运行vue指令解决方案)
  Vue经典面试题: Vue.use和Vue.prototype.$xx有血缘关系吗? -
  Vue自定义指令的用法
  Vue开发常用的框架及案例
  JavaScript Error(错误) 对象
  HTML 头部的元素的用法
  JavaScript 库
  JavaScript Array(数组)对象
  JavaScript 严格模式(use strict)
  jQuery Mobile 简介
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。