简体   繁体   English

CORS无法识别我前端的IP地址

[英]CORS does not recognize the IP address of my frontend

Good Morning.早上好。 I have a problem with Cors on my backend, Cors indicates that the ip address I whitelist is not the same as the one my frontend uses.我的后端Cors有问题,Cors表示我白名单的ip地址和我前端使用的地址不一样。

This is the service that sends the data to the backend:这是将数据发送到后端的服务:

 import { Injectable } from '@angular/core'; import { Persona } from '../modelos/persona'; import { Observable, throwError } from 'rxjs'; import { catchError, tap, map } from 'rxjs/operators'; import {HttpClient,HttpErrorResponse,HttpHeaders }from '@angular/common/http' @Injectable({ providedIn: 'root', }) export class PersonaService { headers=new HttpHeaders(); selectedPersona: Persona; personas:Persona[]; readonly URL_API='http://localhost:3000/Inicio/Persona'; constructor(private http: HttpClient) { this.selectedPersona=new Persona(); } postPersona(persona:Persona){ return this.http.post(this.URL_API,persona) }

The request comes from http://209.145.52.133:8080 which is my front请求来自http://209.145.52.133:8080这是我的前台

In my Backend I set the allowed address:在我的后端,我设置了允许的地址:

 app.use(cors({origin:'http://209.145.52.133:8080'}));

And in the end it returns the error:最后它返回错误:

Access to XMLHttpRequest at 'http://localhost:3000/Inicio/Login' from origin 'http://209.145.52.133:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://209.145.52.133/' that is not equal to the supplied origin.从来源'http://209.145.52.133:8080'访问'http://localhost:3000/Inicio/Login'的XMLHttpRequest已被CORS策略阻止:对预检请求的响应未通过访问控制检查: 'Access-Control-Allow-Origin' header 的值 'http://209.145.52.133/' 不等于提供的来源。

This may help you这可能对你有帮助

app.use((req, res, next) => {
        res.header('Access-Control-Allow-Origin', req.headers.origin);
        res.header(
            'Access-Control-Allow-Headers',
            'Origin, X-Requested-With, Content-Type,  Accept, Authorization',
        );
        res.header(
            'Access-Control-Allow-Methods',
            'GET,POST,fetch, PATCH,OPTIONS,DELETE',
        );
        res.header('Access-Control-Allow-Credentials', true);

        if (req.method === 'OPTIONS') {
            res.header(
                'Access-Control-Allow-Methods',
                'PUT, POST, PATCH,FETCH, DELETE, GET',
            );
            return res.json({});
        } else {
            next();
        }
    });

If not you can try this one from express cors documentation如果没有,您可以从 express cors文档中尝试这个

var corsOptions = {
  origin: 'http://example.com',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}

app.get('/products/:id', cors(corsOptions), function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for only example.com.'})
})

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

相关问题 Nodemailer:我的前端网站无法通过来自不同 IP 地址的节点邮件发送电子邮件 - Nodemailer: My frontend website can't send emails through node mailer from a different IP address 为什么req.socket.address()。address显示的IP地址与实际的公共IP地址不同? - Why does req.socket.address().address show a different IP address than my actual public IP address? 如何在cors Dynamic Origin中获取IP地址? - how to get ip address inside cors Dynamic Origin? JS-在用户IP地址下代理CORS请求? - JS - Proxy a CORS Request under the users IP Address? 为什么CORS策略会阻止我的IP进入我的API? - Why do CORS policies block my ip from my API? nginx没有设置远程客户端ip地址 - nginx does not set remote clients ip address 无法在我的公共 ip 地址上运行 nodejs - Could not run nodejs on my public ip address 前端 package.json 中的代理配置如何请求 void cors? - How does a proxy config in frontend package.json a void cors request? ip.address()在aws上不起作用。 如何以编程方式获取AWS EC2的IP地址? - ip.address() does not work on aws. How to get the IP address of AWS EC2 programmatically? 我的EC2实例中的IP地址解析错误吗? - Is the IP address resolution wrong in my EC2 instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM