代码之家  ›  专栏  ›  技术社区  ›  Abdul Manaf

特快专递申请的访问来源问题

  •  -1
  • Abdul Manaf  · 技术社区  · 6 年前

    我创建了一个带有角度ui的express应用程序。我有以下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.
    
    4 回复  |  直到 6 年前
        1
  •  0
  •   Sanchonn    6 年前

    尝试一次设置多个字段,传递一个对象作为参数。例如, res.set({ "Access-Control-Allow-Origin", "*", "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" });

        2
  •  -1
  •   Abhi    6 年前

    enter image description here

    或者

    chrome.exe--user data dir=“C:/chrome dev session”--禁用web安全

        3
  •  -1
  •   Sunil Singh    6 年前

    实际上,这个问题必须从服务器端(API)解决。如果您在本地测试或者您在不同的域中,那么您必须允许来自API实现的cor。

        4
  •  -1
  •   paras shah    6 年前

    您可以使用chrome的CORS扩展进行开发