webpackdevserver跨域请求配置?

发布于 2022-09-04 09:33:42 字数 1303 浏览 22 评论 0

new WebpackDevServer(webpack(config), {

publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
stats: { colors: true },
proxy:{
  '/orxxxxxxx': {
    target: 'https://api.xxxxxx/2.0',
    secure: false
  }
}

}).listen(3000, 'localhost', function (err, result) {

if (err) {
    console.log(err);
}
console.log('server start');

});
跨域请求总会报这样的错误
XMLHttpRequest cannot load https://api.xxxxxxxx/2.0/orxx... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

求大神帮忙

补充
我的ajax这样写的

$.ajax({

      url:'https://api.xxxxxxx/2.0/orxxxxxxxxx/xxxx',
      type: 'POST',
      dataType: 'json',
      data: values,
      success: function(map){
        var login = map.error;
        if (login='') {
          window.location.href="/dist/about/index.html";
        }
      }
    }) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↙厌世 2022-09-11 09:33:42

你的请求没有带上cors的参数
或者后台不允许跨域
反正跟webpack proxy没关系

更新一下,
你jquery ajax跨域没打开

 xhrFields: {
        withCredentials: true
    },

    crossDomain:true,
仅此而已 2022-09-11 09:33:42

proxy: {

        '/msg/*': {
            target: 'xxxx',
            secure: false,
            changeOrigin : true
        }
   }
瞎闹 2022-09-11 09:33:42

你好,配置问题解决了吗?求教..

橪书 2022-09-11 09:33:42

关注一下,我是用nginx反向代理解决的,nginx配置如下:

    upstream tomcat {
        server localhost:8880;
    }
    
    upstream dora {
        server localhost:8000;
    }
    
    server {
        listen   80; ## listen for ipv4; this line is default and implied
        
        # Make site accessible from http://localhost/
        server_name localhost;
        
        location /api_v1/ {
            proxy_pass http://tomcat;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
        location / {
            proxy_pass http://dora;
            #proxy_set_header Host $host:8888;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文