【发布时间】:2021-12-28 11:56:36
【问题描述】:
使用 NodeJS 作为后端,使用 React 作为前端。前端使用 axios 来做 post/get 请求。使用 localhost:5000 作为服务器,使用 localhost:3000 作为客户端,一切都在开发中运行良好。
但是,当我将服务器和客户端部署到网站时,它们无法工作。我已将各自的本地主机地址更改为网站的地址,但无济于事。以下是我设置后端代码的方式:
const server = express();
server.use(cors({origin: "https://frontendwebsitename.com", credentials: true}));
server.use(express.json());
server.use(express.urlencoded({extended:true}))
在我的前端网站控制台中,我收到此错误: 从源“https://frontendwebsitename.com”访问“https://backendwebsitename.herokuapp.com/fetchDefaultSearch”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问” -Control-Allow-Origin' 标头存在于请求的资源上。
有什么想法吗?
【问题讨论】:
标签: node.js reactjs axios cors access-control