[英]“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):
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
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}}
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: true
和responseType: "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.