我用express创建了以下代理:
import express from "express";
import cors from "cors";
import proxy from "express-http-proxy";
const app = express();
app.use(
cors({
origin: "http://localhost:8081",
credentials: true,
})
);
app.use("/", proxy("http://my-website:8810"));
app.listen(3000, () => {
console.log("server listening on port 3000");
});在前面,我使用的是axios:
axios.defaults.withCredentials = true;
const res = await axios.get("http://localhost:3000", {
auth: {
username: "xxxxx",
password: "xxxxx",
},
headers: {
"Content-Type": "application/json",
},
});但我仍然有以下的cors问题:
从源“
”访问“XMLHttpRequest”(从“http://localhost:3000/”重定向)的“http://localhost:8081”已被CORS策略阻止:对飞行前请求的响应不通过访问控制检查:请求的资源上不存在“访问控制-允许-原产地”标题。
在面对这个问题很多小时后..。我在这里有人能向我解释我在这里做错了什么吗?
发布于 2020-06-15 07:39:21
最后,它通过使用“请求”来工作,类似于:
app.use(
cors({
origin: "http://localhost:8081",
credentials: true,
})
);
router.get("/", (req, res) => {
request(
"http://my-website:8810",
{
auth: {
username: "node_proxy",
password: "password",
},
headers: { Accept: "application/json" },
},
(error, response, body) => {
res.send(body);
}
);
});https://stackoverflow.com/questions/62333128
复制相似问题