vue前后端分离,前端通过axios代理调后端接口,报错404(解决办法)

  • Post author:
  • Post category:vue


解决方法:


方法1:


首先查看自己的配置是否正确,地址信息是否正确


方法2:


重新运行项目


方法3:


向后端寻求帮助,可能不是前端的问题


相关配置

//vue.config.js
const path = require("path");
const resolve = (dir) => {
    return path.join(__dirname, dir);
};

module.exports = {
    publicPath: "/",
    // publicPath: "/auth/",
    assetsDir: "static",
    lintOnSave: false,
    productionSourceMap: false, // 除去.map文件
    chainWebpack: (config) => {
        config.resolve.alias.set("@", resolve("src"));
    },

    devServer: {
        open: true, //是否自动弹出浏览器页面

        proxy: {
            "/api": {
                target: "http://......", //API服务器的地址,后端接口地址
                pathRewrite: {
                    //重写路径
                    "^/api": "",
                },
            },
        },
    },
};
//axios.js
axios.defaults.baseURL =
    process.env.NODE_ENV == "development" ?
    "/api" :
    "http://" + window.location.host;



版权声明:本文为weixin_38345306原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。