最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決axios:"timeout of 5000ms exceeded"超時(shí)的問題

 更新時(shí)間:2022年08月03日 09:48:28   作者:前端@小菜  
這篇文章主要介紹了解決axios:"timeout of 5000ms exceeded"超時(shí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

axios:"timeout of 5000ms exceeded"超時(shí)

最近遇到一個(gè)問題,在我開機(jī)后,啟動(dòng)后臺(tái)服務(wù)器登錄程序時(shí)會(huì)報(bào)請(qǐng)求超時(shí)的問題。網(wǎng)上找了下解決方法,最后成功解決。

首先,我們要查看自己的請(qǐng)求地址是否正確,后端是否正常開啟,數(shù)據(jù)庫是否啟動(dòng);若都正確無誤,則繼續(xù)往下看。

在看以下代碼時(shí),大家可以參考我的另一篇文章:vue開發(fā)中 axios 的封裝

注:我使用的是 0.18.1 版本,0.19.0 版本似乎有問題,見:https://github.com/ly2011/blog/issues/159 中的評(píng)論。

具體代碼如下: 

import axios from 'axios'
import { BASE_URL } from './http'
import router from '../router'
?
// create an axios instance
const service = axios.create({
? baseURL: BASE_URL, // url = base url + request url
? // withCredentials: true, // send cookies when cross-domain requests
? timeout: 5000 // request timeout
})
?
?
// 設(shè)置請(qǐng)求次數(shù),請(qǐng)求的間隙
service.defaults.retry = 4;
service.defaults.retryDelay = 1000;
?
// request interceptor
service.interceptors.request.use(
? config => {
? ? // do something before request is sent
? ? return config
? },
? error => {
? ? // do something with request error
? ? // console.log(error) // for debug
? ? return Promise.reject(error)
? }
)
?
// response interceptor
service.interceptors.response.use(
? response => {
? ? const res = response.data
? ? return res
? },
? error => {
? ? if (error.response) {
? ? ? // console.log('err' + error) // for debug
? ? ? switch (error.response.status) {
? ? ? ? case 401:
? ? ? ? ? // console.log('err status' + error.response.status)
? ? ? ? ? router.push('/login')
? ? ? ? ? break
? ? ? ? case 404:
? ? ? ? ? break
? ? ? ? case 500:
? ? ? ? ? break
? ? ? }
? ? ? return Promise.reject(error)
? ? } else {
? ? ? // 處理超時(shí)的情況
? ? ? let config = error.config
? ? ? // If config does not exist or the retry option is not set, reject
? ? ? if(!config || !config.retry) return Promise.reject(error)
??
? ? ? // Set the variable for keeping track of the retry count
? ? ? config.__retryCount = config.__retryCount || 0
??
? ? ? // Check if we've maxed out the total number of retries
? ? ? if(config.__retryCount >= config.retry) {
? ? ? ? // Reject with the error
? ? ? ? return Promise.reject(error)
? ? ? }
??
? ? ? // Increase the retry count
? ? ? config.__retryCount += 1
??
? ? ? // Create new promise to handle exponential backoff
? ? ? let backoff = new Promise(function(resolve) {
? ? ? ? setTimeout(function() {
? ? ? ? ? resolve()
? ? ? ? }, config.retryDelay || 1)
? ? ? })
??
? ? ? // Return the promise in which recalls axios to retry the request
? ? ? return backoff.then(function() {
? ? ? ? return service(config)
? ? ? })
? ? }
?
? }
)
?
export default service

當(dāng)請(qǐng)求超時(shí)后,axios 將重新發(fā)起請(qǐng)求,請(qǐng)求次數(shù)和間隙可以自己設(shè)定。

這里我創(chuàng)建了一個(gè) axios 實(shí)例,所有 api 接口都通過這個(gè)實(shí)例進(jìn)行請(qǐng)求。

如果你不想這樣做,可以像下面這樣寫:

//在main.js設(shè)置全局的請(qǐng)求次數(shù),請(qǐng)求的間隙
axios.defaults.retry = 4;
axios.defaults.retryDelay = 1000;
?
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
? ? var config = err.config;
? ? // If config does not exist or the retry option is not set, reject
? ? if(!config || !config.retry) return Promise.reject(err);
?
? ? // Set the variable for keeping track of the retry count
? ? config.__retryCount = config.__retryCount || 0;
?
? ? // Check if we've maxed out the total number of retries
? ? if(config.__retryCount >= config.retry) {
? ? ? ? // Reject with the error
? ? ? ? return Promise.reject(err);
? ? }
?
? ? // Increase the retry count
? ? config.__retryCount += 1;
?
? ? // Create new promise to handle exponential backoff
? ? var backoff = new Promise(function(resolve) {
? ? ? ? setTimeout(function() {
? ? ? ? ? ? resolve();
? ? ? ? }, config.retryDelay || 1);
? ? });
?
? ? // Return the promise in which recalls axios to retry the request
? ? return backoff.then(function() {
? ? ? ? return axios(config);
? ? });
});

參考鏈接:https://github.com/axios/axios/issues/164

報(bào)錯(cuò) Error: timeout of 5000ms exceeded

在確定后端代碼沒有問題,鎖定前端

修改 \src\utils 目錄下的 request.js

修改timeout屬性值

有需要以后再來優(yōu)化

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue項(xiàng)目完成后如何實(shí)現(xiàn)項(xiàng)目優(yōu)化的示例

    vue項(xiàng)目完成后如何實(shí)現(xiàn)項(xiàng)目優(yōu)化的示例

    本文主要介紹了vue項(xiàng)目完成后如何實(shí)現(xiàn)項(xiàng)目優(yōu)化的示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 淺談Vue static 靜態(tài)資源路徑 和 style問題

    淺談Vue static 靜態(tài)資源路徑 和 style問題

    這篇文章主要介紹了淺談Vue static 靜態(tài)資源路徑 和 style問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue3+vite中使用import.meta.glob的操作代碼

    vue3+vite中使用import.meta.glob的操作代碼

    在vue2的時(shí)候,我們一般引入多個(gè)js或者其他文件,一般使用? require.context 來引入多個(gè)不同的文件,但是vite中是不支持 require的,他推出了一個(gè)功能用import.meta.glob來引入多個(gè),單個(gè)的文件,下面通過本文介紹vue3+vite中使用import.meta.glob,需要的朋友可以參考下
    2022-11-11
  • Vue組件生命周期三個(gè)階段全面總結(jié)講解

    Vue組件生命周期三個(gè)階段全面總結(jié)講解

    Vue的生命周期就是vue實(shí)例從創(chuàng)建到銷毀的全過程,也就是new Vue() 開始就是vue生命周期的開始。Vue 實(shí)例有?個(gè)完整的?命周期,也就是從開始創(chuàng)建、初始化數(shù)據(jù)、編譯模版、掛載Dom -> 渲染、更新 -> 渲染、卸載 等?系列過程,稱這是Vue的?命周期
    2022-11-11
  • vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式

    vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式

    這篇文章主要介紹了vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用Vue.js和MJML創(chuàng)建響應(yīng)式電子郵件

    使用Vue.js和MJML創(chuàng)建響應(yīng)式電子郵件

    這篇文章主要介紹了使用Vue.js和MJML創(chuàng)建響應(yīng)式電子郵件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 詳解vue通過NGINX部署在子目錄或者二級(jí)目錄實(shí)踐

    詳解vue通過NGINX部署在子目錄或者二級(jí)目錄實(shí)踐

    這篇文章主要介紹了詳解vue通過NGINX部署在子目錄或者二級(jí)目錄實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Vue package.json配置深入分析

    Vue package.json配置深入分析

    這篇文章主要介紹了Vue package.json配置,package.json是每個(gè)前端項(xiàng)目都會(huì)有的json文件,位于項(xiàng)目的根目錄中。很多腳手架在創(chuàng)建項(xiàng)目的時(shí)候會(huì)幫我們自動(dòng)初始化好 package.json
    2023-01-01
  • vue3搭配pinia的踩坑實(shí)戰(zhàn)記錄

    vue3搭配pinia的踩坑實(shí)戰(zhàn)記錄

    Pinia是一個(gè)同時(shí)支持Vue2和Vue3的應(yīng)用狀態(tài)管理工具,簡單來說就是為了管理整個(gè)應(yīng)用中的響應(yīng)式數(shù)據(jù),解決各個(gè)組件交互時(shí)數(shù)據(jù)狀態(tài)的不好管理的問題,下面這篇文章主要給大家介紹了關(guān)于vue3搭配pinia踩坑的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Vue中的反向代理

    Vue中的反向代理

    這篇文章主要介紹了Vue中的反向代理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01

最新評(píng)論

岳西县| 南昌市| 宜兰市| 永平县| 武汉市| 京山县| 安阳市| 宝坻区| 堆龙德庆县| 汨罗市| 南木林县| 视频| 皋兰县| 天门市| 方山县| 康乐县| 义乌市| 竹北市| 西乡县| 阳春市| 凌源市| 长岛县| 灌南县| 井冈山市| 唐山市| 古田县| 高密市| 镇安县| 吴旗县| 新晃| 南皮县| 虎林市| 繁昌县| 乌兰浩特市| 九台市| 巴中市| 固镇县| 湘潭市| 成都市| 利川市| 揭阳市|