str*_*rks 3 node.js express next.js
在我的浏览器开发人员工具栏中,我收到以下 POST 请求的错误消息:
Access to XMLHttpRequest at 'http://localhost:8000/api/tags/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
但是,当我查看 server.js 时,我确实允许访问:
app.prepare().then(() => {
const server = express();
server.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么现在被阻止
小智 7
最好使用“cors”包,如下所示。
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
// some route controllers
const customRoute = require('./customRoute.controller');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Custom routes
app.use('/api/tags', customRoute);
app.use(express.static(path.join(__dirname, 'dist')));
// Catch all other routes & return index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
module.exports = app;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25875 次 |
| 最近记录: |