How to overcome “Access-Control-Allow-Origin” error when client talks to server

后端 未结 2 590
一向
一向 2021-01-21 09:56

So I\'m using a yeoman project from swiip called generator-gulp-angular - just do \"npm search gulp-angular\" and you\'ll see it.

Out of the box the client is running fr

相关标签:
2条回答
  • 2021-01-21 10:33

    You have to setup a proxy server to forward your requests, setup a reverse proxy, or setup CORS on the back-end to allow the cross-origin request.

    0 讨论(0)
  • 2021-01-21 10:48

    Some information on the erroneous request/response would be helpful too.

    You could try to set the changeOrigin param to true. This will modify the request's host header to match the server's hostname.

    var proxyMiddleware = require('http-proxy-middleware');
    
    var options = {
      target: 'http://127.0.0.1:8080',
      changeOrigin: true         // <-- changeOrigin
    };
    

    If that doesn't work; you can add the Access-Control-Allow-Origin header to the response:

    var proxyMiddleware = require('http-proxy-middleware');
    
    var options = {
      target: 'http://127.0.0.1:8080',
      onProxyRes: function (proxyRes, req, res) {
        proxyRes.headers['Access-Control-Allow-Origin'] = '*';
      }
    };
    

    http-proxy-middleware onProxyRes option is added in v0.5.0 , so make sure to update it if you're still using v0.0.5

    0 讨论(0)
提交回复
热议问题