Access to XMLHttpRequest at '…' from origin '…' has been blocked by CORS policy


Access to XMLHttpRequest at '…' from origin '…' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

服务器不允许跨域访问。跨域访问发生在客户端和服务器,而服务器和服务器间不会发生跨域访问问题,所以解决办法是使用请求代理。

在项目根目录下新建vue.config.js文件,该文件在老版vue中自带,但认为并不是每个项目中都会用到,所以改为了内置(名字和位置必须正确,否则vue不识别该文件),现在要用的话需要自行创建。

module.exports = {
    //服务器配置
    devServer: {
        //代理
        proxy: {
            '/api': {
                //只要输入/api(该路径可自定义)会自动重定向(替换)为target中的地址(服务器地址)
                target: 'https://api.binstd.com/recipe/search',
                //changeOrigin:true 允许跨域
                changeOrigin: true,
                //路径重写,/api是自定义路径,真正项目中是没有这个路径的,所以要去掉
                pathRewrite: {
                    '^/api': ''
                }
            }
        }
    }
}

修改后记得要重启vue项目以便生效。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Access to XMLHttpRequest at *** from origin‘***' has been blocked by CORS policy: Response to preflight request ... Access to XMLHttpRequest at xxxx from origin ‘null‘ has been blocked by CORS policy: ElementUI项目请求SpringBoot后台项目时提示:Access to XMLHttpRequest at **from origin ** has been blocked by CORS policy Access to XMLHttpRequest at '6%E6E6%B%9A%9' from origin from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol scheme Access to XMLHttpRequest at 'XXX' from origin 'XX' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present o AJAX跨域请求解决方法 Access to XMLHttpRequest at '*url*' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. GET *url* net::ERR_FAILED Access to XMLHttpRequest at 'http://localhost:8090/user/getotp' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 【跨域问题】Access to XMLHttpRequest at ‘A’from ‘B’has been blocked by CORS policy :... No ‘Access-Control-Allow-Origin’ header...(Chrome浏览器) VUE 出现Access to XMLHttpRequest at 'http://192.168.88.228/login/Login?phone=19939306484&password=111' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Contr error - image - Access to image at '' from origin 'null' has been blocked by CORS policy:
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM