ExpressJs应用程序中的cors js正在阻止应通过的请求
我相信我正在正确设置我的express应用程序的设置,但我的请求仍然失败 快速Cors设置:ExpressJs应用程序中的cors js正在阻止应通过的请求,express,cors,fetch,Express,Cors,Fetch,我相信我正在正确设置我的express应用程序的设置,但我的请求仍然失败 快速Cors设置: app.use( cors({ origin: ["localhost"] }) ); app.options("*", cors()); // include before other routes 也试过 app.use( cors() ); app.options("*", cors()); // include before other routes 请求:
app.use(
cors({
origin: ["localhost"]
})
);
app.options("*", cors()); // include before other routes
也试过
app.use(
cors()
);
app.options("*", cors()); // include before other routes
请求:
fetch(`localhost:5000/charge`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: (
JSON.stringify({
stripeToken: token.id,
chargeAmount: Math.round(100 * this.state.donationAmount) // Needs to be an integer in cents
})
)
})
.then(res => {
return res.text();
})
.then(txt => {
console.log(txt);
})
浏览器错误:
CORS策略已阻止从源“”获取“”的访问:请求的资源上不存在“访问控制允许源”标头。如果不透明响应满足您的需要,请将请求的模式设置为“no cors”,以获取禁用cors的资源
你知道这里失败的是什么吗?我需要响应,因此没有COR对我无效。尝试添加前端的完整url,并添加如下方法:
app.use(cors({
origin: ['http://localhost:5000'],
methods: ['GET', 'POST', 'PUT', 'DELETE'],
credentials: true // enable set cookie
}));
原点只是一个字符串,不是正则表达式。RegEx必须这样定义:
源代码:[/.*localhost.*/]
尝试为fetch req添加.catch()
,并查看您得到的错误信息。