[英]Node.js: No 'Access-Control-Allow-Origin' header is present on the requested resource
I'm newer to cors and exactly what you need.我对 cors 较新,这正是您所需要的。 I had an app working live a couple of months ago and it seems like it has broken due to CORS.
几个月前我有一个应用程序正在运行,它似乎由于 CORS 而损坏。 Every time I make a request I get the error: "as been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
每次我提出请求时,我都会收到错误消息:“被 CORS 政策阻止:请求的资源上不存在‘Access-Control-Allow-Origin’ header。”
Here is my server cors code:这是我的服务器 cors 代码:
const express = require("express");
const jwt = require("jsonwebtoken");
const cors = require('cors');
const app = express();
// Set Port
app.set("port", process.env.PORT || 5000);
// Listen
app.listen(process.env.PORT || 5000);
// Load Middlewear
app.use(express.urlencoded({ extended: true }));
app.use(express.json()); // To parse the incoming requests with JSON payloads
// CORS HEADERS MIDDLEWARE
var corsOptions = {
origin: '*',
optionsSuccessStatus: 200,
}
app.use(cors(corsOptions));
You need to allow the back to accept only by the front like this您需要像这样只允许背面接受正面
`` var corsOptions = { origin: 'http://localhost:8080' front ip or dns } `` var corsOptions = { origin: 'http://localhost:8080' 前面 ip 或 dns }
app.use(cors(corsOptions) `` app.use(cors(corsOptions) ``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.