简体   繁体   English

无法在NODE.JS中解决此问题,请求的资源在(nodejs + angularjs)中不存在“ Access-Control-Allow-Origin”标头

[英]Unable to solve this in NODE.JS, No 'Access-Control-Allow-Origin' header is present on the requested resource in (nodejs + angularjs)

I am new to node.js and trying to make Rest API, now my REST API working fine but whenever i called this services through angularjs, it gives me "access-control-allow-origin" error. 我是node.js的新手,正在尝试使用Rest API,现在我的REST API可以正常工作,但是每当我通过angularjs调用此服务时,都会出现“ access-control-allow-origin”错误。 However, i added this code on server side :- 但是,我在服务器端添加了此代码:-

var express = require("express");  
var app = express();
var router = express.Router();

app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Access-Control-Allow-Credentials', true);
    return next();
});

But its not working for me, i searched a lot but didn't get feasible solution, please help me out. 但是它对我不起作用,我进行了很多搜索,但没有获得可行的解决方案,请帮帮我。

Error is :- 错误是:- 在此处输入图片说明

You are using http module of angular, if yes, then Angular sends a pre-flight options before actual request. 您正在使用angular的http模块,如果是,则Angular在实际请求之前发送预检选项。 Your server should return 20X for OPTIONS for angular to make actual request. 您的服务器应返回20倍的OPTIONS角度,以发出实际的请求。

You can avoid this by handling OPTIONS separately , 您可以通过分别处理OPTIONS来避免这种情况,

app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Access-Control-Allow-Credentials', true);


  if (req.method == "OPTIONS") {
          response.statusCode = 200 //although correct code should be 204,
      return;
        }

    return next();
});

Also a more exhaustive and less restrictive CORS filter will look like below, use it as per your need 另外,更详尽,限制更少的CORS过滤器如下所示,请根据需要使用

            resp.header("Access-Control-Allow-Origin", "*");
            resp.header("Access-Control-Allow-Credentials", "true");
            resp.header("Access-Control-Allow-Methods", "POST, GET, PUT,PATCH, OPTIONS, DELETE");
            resp.header("Access-Control-Max-Age", "3600");
            resp.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers");

            resp.header("Access-Control-Expose-Headers", "Authorization");

The browser sends a preflight request before making the actual request. 浏览器在发出实际请求之前先发送preflight请求。 You need to add code for handling this requests, Try this in your server side code. 您需要添加代码来处理此请求,请在服务器端代码中尝试此操作。

const cors = require('cors')
const corsOptions = {
  origin: '*'
}
app.use(cors(corsOptions))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请求的资源 nodejs 上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource nodejs AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - AngularJS: No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头。 NodeJS 和 AngularJS 应用 - No 'Access-Control-Allow-Origin' header is present on the requested resource. NodeJS and AngularJS App IISNOde中的请求资源(AngularJS + NodeJs)上没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource (AngularJS + NodeJs) In IISNOde AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 - No 'Access-Control-Allow-Origin' header is present on the requested resource. 角度:“ Access-Control-Allow-Origin”标头出现在请求的资源上 - Angular: 'Access-Control-Allow-Origin' header is present on the requested resource 登录时请求的资源上没有“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource on login 请求的资源上没有“ Access-Control-Allow-Origin”标头-错误 - No 'Access-Control-Allow-Origin' header is present on the requested resource - error 尽管存在请求的资源,但没有“ Access-Control-Allow-Origin”标头存在 - No 'Access-Control-Allow-Origin' header is present on the requested resource despite it is there
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM