欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VUE之axios解决跨域方案

程序员文章站 2022-03-21 18:37:33
跨域示例Access to XMLHttpRequest at ‘xxxx’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource....
跨域示例

VUE之axios解决跨域方案
Access to XMLHttpRequest at ‘xxxx’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

解决


在vue.config.js中(vue-cli3之后需要自己建)
	   devServer: {
        host: 'localhost',
        port: 8080, // 端口
        https: false,
        proxy: {
            // 配置跨域

            '/api': {
                target: '目标域名 如 http://baidu.com',  // 代理的接口域名以及端口号;
                ws: true,   // 支持ws协议;websocket的缩写;
                changeOrigin: true,// 是否跨域
                pathRewrite: {     // 路径替换
                    '^/api': ''
                }
            }
        },
        hotOnly: false,
        before: app => { }
    },
接口调用
  this.$axios({
      url: "index",
      method: "get",
      params: {},
    }).then((res) => {
      console.log(res);
    });

本文地址:https://blog.csdn.net/AIB_Kasic/article/details/107866846

相关标签: VUE