webpack dev服务器CORS问题 [英] webpack dev server CORS issue
本文介绍了webpack dev服务器CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 webpack-dev-server v1.10.1
来提升我的Redux项目,我有以下选项:
I am using webpack-dev-server v1.10.1
to boost up my Redux project and I have the options below:
contentBase: `http://${config.HOST}:${config.PORT}`,
quiet: false,
noInfo: true,
hot: true,
inline: true,
lazy: false,
publicPath: configWebpack.output.publicPath,
headers: {"Access-Control-Allow-Origin": "*"},
stats: {colors: true}
在JS中,我使用从
superagent
中的请求
生成HTTP GET调用
In the JS, I am using request
from superagent
to generate a HTTP GET call
request
.get(config.APIHost + apiUrl)
.set('Accept', 'application/json')
.withCredentials()
.end(function (err, res) {
if (!err && res.body) {
disptach(() => {
return {
type: actionType || GET_DATA,
payload: {
response: res.body
}
}
});
}
});
但是我得到了CORS错误:
But I got the CORS error:
XMLHttpRequest cannot load http://localhost:8000/api/getContentByType?category=all. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5050' is therefore not allowed access
任何建议要解决这个问题?非常感谢。
Any suggestion to resolve this? Thanks a lot
推荐答案
使用webpack-dev-server 1.15.X你可以在配置文件中使用这个配置:
With webpack-dev-server 1.15.X you can use this configuration in your config file:
devServer: {
contentBase: DIST_FOLDER,
port: 8888,
// Send API requests on localhost to API server get around CORS.
proxy: {
'/api': {
target: {
host: "0.0.0.0",
protocol: 'http:',
port: 8080
},
pathRewrite: {
'^/api': ''
}
}
}
},
< 0.0:8888 / api / * 到 http://0.0.0.0:8080/*
并且CORS解决了
With this example you will redirect all calls from http://0.0.0.0:8888/api/*
to http://0.0.0.0:8080/*
and CORS solved
这篇关于webpack dev服务器CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文