axios設置請求超時時間 timeout

1.axios全局設置網絡超時

axios.defaults.timeout = 30 * 1000; // 30s

2. 單獨對某個請求設置網絡超時

axios.post(url, params, {timeout: 1000})
  .then(res => {
    console.log(res);
  })
  .catch(err=> {
    console.log(err);
  })
})

3.webpack的dev的proxyTable的超時時間設置

  dev: {
    // Paths
    assetsSubDirectory: 'static', // 靜態資源文件夾
    assetsPublicPath: '/', // 發佈路徑
    // 代理配置表,在這裏可以配置特定的請求代理到對應的API接口
    // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
    proxyTable: {
      '/api': {
        timeout: 30000, // 請求超時時間
        target: 'http://127.0.0.1:3006', // 目標接口域名
        changeOrigin: true, // 是否跨域
        pathRewrite: {
          '^/api': '' // 重寫接口
        }
      },
    // Various Dev Server settings
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
  },

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章