简体   繁体   English

“CORS:不存在‘Access-Control-Allow-Origin’header”,但存在

[英]“CORS: No 'Access-Control-Allow-Origin' header is present ”, but it is present

I'm trying to log in or register in my Angular app, but whenever I access to the php file which does the HTTP calls to the DB regarding users (authentication.php), I get three errors:我正在尝试在我的 Angular 应用程序中登录或注册,但是每当我访问 php 文件时,该文件执行 HTTP 调用数据库时,会收到三个关于用户的错误(authentication.php):

  1. Access to XMLHttpRequest at 'http://localhost/authentication.php?command=login' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

  2. ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"http://localhost/authentication.php","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost/authentication.php: 0 Unknown Error","error":{"isTrusted":true}}

  3. GET http://localhost/authentication.php?command=login net::ERR_FAILED

(The last one says GET for login, POST for register) (最后一个说 GET 用于登录,POST 用于注册)

In authentication.php I have the following code regarding headers:authentication.php我有以下关于标头的代码:

require "openDB.php";
include "validators/usuarioValidator.php";

In openDB.php :openDB.php

header('Access-Control-Allow-Origin: http://localhost:4200');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: PUT, DELETE, POST, GET');

In validators/usuarioValidator.php :validators/usuarioValidator.php

header('Access-Control-Allow-Origin: http://localhost:4200');
header('Access-Control-Allow-Headers: *');
header('Access-Control-Allow-Methods: PUT, DELETE, POST, GET');

I've tried writing those headers in authentication.php , adding OPTIONS to Allow-Methods and allow all origins with *, but I still get the same three errors.我尝试在authentication.php中编写这些标头,将 OPTIONS 添加到 Allow-Methods 并允许使用 * 的所有来源,但我仍然遇到相同的三个错误。

My user.service.ts has the following code regarding these operations:我的user.service.ts有以下关于这些操作的代码:

register(user : any, key : string) : Promise<any>{
    let parametros = new HttpParams().set("command", "register");

    user.verif = key;

    return this.http.post(this.url, user, {params: parametros}).toPromise();
  }

(I'm just pasting register because login is a bit weirdly done and the problem here seems to be with authentication.php , no matter what operation it makes) (我只是粘贴注册,因为登录有点奇怪,这里的问题似乎与authentication.php ,不管它做什么操作)

I've also tried adding withCredentials: true and responseType: "blob" to params since I'm not receiving a json in the TS, but still nothing.我还尝试将withCredentials: trueresponseType: "blob"添加到参数,因为我没有在 TS 中收到 json,但仍然没有。

It seems like you are not setting Headers properly in your HTTP Request body, try the following code,您似乎没有在 HTTP 请求正文中正确设置标头,请尝试以下代码,

register(user : any, key : string) : Promise<any>{
    let parametros = new HttpParams().set("command", "register");

    user.verif = key;

    return this.http.post(this.url, user, {Headers: parametros}).toPromise();
  }

Also, if you use the HTTPClient package to make requests then it's a bit hard to set custom headers .此外,如果您使用HTTPClient package 发出请求,那么设置自定义标头有点困难。 If you can prevent some issues (like CORS Access-Control Errors) by using axios or a similar package如果您可以通过使用axios或类似的 package 来防止某些问题(如CORS访问控制错误)

暂无
暂无

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

相关问题 CORS“没有&#39;Access-Control-Allow-Origin&#39;标题存在”但是有 - CORS “No 'Access-Control-Allow-Origin' header is present” yet there is 不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present 离子应用CORS,不带&#39;Access-Control-Allow-Origin&#39;标头,将Lumen API与CORS中间件一起使用 - Ionic app CORS No 'Access-Control-Allow-Origin' header is present, using Lumen API with CORS middleware CORS没有&#39;Access-Control-Allow-Origin&#39;标头存在,但OPTIONS和POST请求却得到200,但 - CORS Getting No 'Access-Control-Allow-Origin' header is present but OPTIONS and POST request get 200 but Yii2 cors 过滤错误,即不存在“Access-Control-Allow-Origin”标头 - Yii2 cors filters error that No 'Access-Control-Allow-Origin' header is present CORS问题-“所请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头。” - CORS-issue - “No 'Access-Control-Allow-Origin' header is present on the requested resource.” 错误:请求的资源上没有“Access-Control-Allow-Origin”标头 - Error: No '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 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No '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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM