Forwarding issues using webpack+dev+server - Stack Overflow
黄舟
黄舟 2017-05-19 10:26:29
0
1
578

Using react to develop projects, my backend colleagues recently provided interfaces. I use fetch to make AJAX requests. But the console reports the following error!

Cross-domain problems have arisen. Some people say that webpack dev server can implement proxy request forwarding, so that cross-domain problems will not occur.
But I followed the official configuration and other people’s configurations online, but it’s still the same and has no effect.
devServer: {

hot: true,
contentBase: resolve(__dirname, ''),
publicPath: '/',
proxy: {
    '/api': {
      target: 'http://test.lailaapp.com',
      pathRewrite: {'^/api' : ''},
      changeOrigin: true
    }
  }

},
My configuration is as above. I don’t know what went wrong. I used the webpack2.0 version to implement hot update according to the official configuration. Can anyone help me explain how to configure it to forward requests and solve cross-domain issues?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
PHPzhong

I made a common sense mistake. Configuring proxy does not mean that requesting the original URL can automatically solve cross-domain problems.

You should change the request address to a local one, that is http://localhost:port/api

For examplehttp://test.lailaapp.com/api/orderPlus/v2/bills 对应的就应该修改为
http://localhost:port/api/orderPlus/v2/bills
当然http://localhost:port可以省略,也就是/api/orderPlus/v2/bills

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template