Cross Origin:如何从本地webpack dev服务器加载模拟数据

时间:2016-11-29 16:36:31

标签: cors webpack-dev-server

我正在一个在本地开发网站www.project.com上运行的网站上开发一个组件。

我还在localhost:3000上运行webpack dev服务器来执行ReactJS模板。

我喜欢使用允许我模拟服务器响应的本地模拟json响应。我可以轻松地在de开发服务器(www.project.com)和模拟json之间切换。对于开发服务器,我需要发送身份验证cookie,因此在我的提取请求中,我启用凭据:'include'。

然而,当我尝试加载我的本地模拟时,我遇到了CORS问题:

Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://www.project.com' is therefore not allowed access.

我尝试了很多不同的东西,没有任何作用。

const headers = {
  'Content-Type'            : 'application/json',
  'X-Requested-With'    : 'XMLHttpRequest'
};

        const response = await fetch(config.getRESTEndpoint(URLS.LOADCANDIDATES), {
            method : 'GET',
            headers : headers,
            credentials: 'include',
        });

server.config

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
    publicPath: config.output.publicPath,
    headers: {
      'Access-Control-Allow-Origin': '*',
      // 'Access-Control-Allow-Credentials', 'true';
      // 'Access-Control-Allow-Methods': 'GET,HEAD,OPTIONS,POST,PUT',
      // 'Access-Control-Allow-Headers': 'X-MSA-Header, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers',
    },
    hot: true,
    historyApiFallback: true,
    quiet: false,
    noInfo: false,
    stats: {
      // Config for minimal console.log mess.
      assets: false,
      colors: true,
      version: false,
      hash: false,
      timings: false,
      chunks: false,
      chunkModules: false
    }
}).listen(3000, 'localhost', function (err) {
    if (err) {
        console.log(err);
    }

  console.log('Listening at localhost:3000');
});

有什么建议吗?

0 个答案:

没有答案