Uncaught (in promise) Error: Request failed with status code 404

一时失言乱红尘 2023-07-21 11:23 23阅读 0赞

20200403180939936.png

用postman调接口有正确的返回值,与后台无关;肯定是前端配置问题;找了好几个小时。

1.main.js中使用到mock的信息注释掉,因为axios与mock会有冲突

  1. // import { mockXHR } from '../mock'
  2. // if (process.env.NODE_ENV === 'production') {
  3. // mockXHR()
  4. // }

2.config下面的index.js中设置proxy代理的代码片段,设置之后要重新npm run dev,webstorm的自动重启不起作用

我的原因就是应为没有重启,/(ㄒoㄒ)/~~

在proxy模块中设置了‘/xxf’,target中设置服务器地址+端口号,然后我们在调用接口的时候,就可以全局使用‘/xxf’,这时候‘/xxf’的作用就相当于一个唯一标识,比如接口地址是 http://192.168.106.8:8888/xxf/deptList,那我们在调用接口时可以直接写为/xxf/deptList,系统会自动识别proxy中/xxf标识中的target地址并拼接在一起。

那pathRewrite是用来干嘛的呢,这里的作用,相当于是替换‘/xxf’,如果接口中没有/xxf,那就直接置空,{‘^/xxf’:‘’},置空后真实的地址就变成了 http://192.168.106.8:8888/xxf/deptList,与真实接口不一致,这就会导致了接口错误,报错404;

如果接口中有/xxf,那就得写成{‘^/xxf’:‘/xxf’},可以理解为一个替换路径、重写路径或者重定向的功能。

或者不设置pathRewrite参数

  1. 'use strict'
  2. // Template version: 1.3.1
  3. // see http://vuejs-templates.github.io/webpack for documentation.
  4. const path = require('path')
  5. module.exports = {
  6. dev: {
  7. // Paths
  8. assetsSubDirectory: 'static',
  9. assetsPublicPath: '/',
  10. proxyTable: {
  11. '/xxf':{
  12. //target: 'https://home.rootensoft.com/api/',
  13. target: 'http://localhost:8888',
  14. secure: true,
  15. changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
  16. pathRewrite: { //路径重置
  17. '^/xxf': '/xxf'
  18. }
  19. }
  20. },
  21. host: '127.0.0.1', // can be overwritten by process.env.HOST
  22. port: 8889, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
  23. autoOpenBrowser: false,
  24. errorOverlay: true,
  25. notifyOnErrors: true,
  26. poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
  27. useEslint: false,
  28. showEslintErrorsInOverlay: false,
  29. devtool: 'cheap-module-eval-source-map',
  30. cacheBusting: true,
  31. cssSourceMap: true
  32. },
  33. build: {
  34. index: path.resolve(__dirname, '../dist/index.html'),
  35. assetsRoot: path.resolve(__dirname, '../dist'),
  36. assetsSubDirectory: 'static',
  37. assetsPublicPath: './',
  38. productionSourceMap: true,
  39. devtool: '#source-map',
  40. productionGzip: false,
  41. productionGzipExtensions: ['js', 'css'],
  42. bundleAnalyzerReport: process.env.npm_config_report
  43. }
  44. }

发表评论

表情:
评论列表 (有 0 条评论,23人围观)

还没有评论,来说两句吧...

相关阅读