我正在尝试通过使用base64进行图像检测从本地上传图像。
并且在本地主机和邮递员中一切正常。
但是在部署之后,我得到了 CROS 错误。
我已经有 cors 中间件了 server.js
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
app.use(cors());
app.use(bodyParser.json({ limit: "10000kb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "10000kb", extended: true }));
cors 中间件在使用 url 获取图像时工作正常,
但是当我尝试使用base64从本地上传图像时,控制台显示:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我尝试过的解决方案:
- cors-任何地方
App.js
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
fetch(proxyUrl + API_CALL.IMAGE_URL, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
inputLink: inputLink,
inputMethod: inputMethod
}),
credentials: 'include'
})
然后显示413 payload too large
。
由于在localhost和postman中测试时没有错误,我发现一些文章说它可能仍然是cors错误。
- CORS 预检
服务器.js
const corsOptions = {
origin: 'https://front-end-url/',
methods: 'GET, POST, PUT',
credentials: true,
allowedHeaders: 'Content-Type,Authorization',
exposedHeaders: 'Content-Range,X-Content- Range'
};
app.options('/imageUrl', cors(corsOptions));
它显示错误:
CORS policy: Response to preflight request doesn't pass access control check:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'
when the request's credentials mode is 'include'
- 删除后
credentials: 'include'
,它413 payload too large
再次显示。
我很困惑......有谁知道如何解决它?谢谢你。