我用angular ui创建了一个快速应用程序。我已经关注了app.js文件
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var userRouter = require('./routes/user');
var app = express();
// view engine setup
app.use(express.static(__dirname + '/dist'));
app.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();
});
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/student', userRouter);
// catch 404 and forward to error handler
app.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(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我遇到这样的错误
request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin. is therefore not allowed access.
答案 0 :(得分:0)
尝试一次设置多个字段,将一个对象作为参数传递。例如,
import { format } from 'date-fns';
答案 1 :(得分:-1)
您可以像这样实现并检查吗?
OR
如果您现在想快速工作,可以使用chrome的hack代码。运行这个
在Windows中,将此命令粘贴到运行窗口中 chrome.exe --user-data-dir =“ C:/ Chrome开发者会话” --disable-web-security
答案 2 :(得分:-1)
实际上,必须从服务器端(API)解决此问题。如果您正在本地进行测试或在其他域中,则必须允许API实施中的CORS。
答案 3 :(得分:-1)
您可以将chrome的CORS扩展名用于开发目的