PHP resource file:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST, DELETE, OPTIONS, GET, PUT");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
require_once ("center_service.php");
$data = json_decode(file_get_contents("php://input"));
$method = $_SERVER['REQUEST_METHOD'];
if (!isset($routes[2])) {
$sql = "SELECT * FROM centers;";
Center::readCenter($sql);
} else if (isset($routes[2]) && is_numeric($routes[2]) && $method === "GET") {
$sql = "SELECT * FROM centers WHERE id='$routes[2]';";
Center::readCenter($sql);
} else if (isset($routes[2]) && is_numeric($routes[2]) && $method === "DELETE") {
Center::deleteCenter($routes[2]);
}
else {
header('HTTP/1.0 404 Not Found');
require '404.php';
}
Angular service file:
readonly ROOT_URL = 'http://localhost/ecdweb/api/index.php';
getCenters(): Observable<Center[]> {
return this.http.get<Center[]>(this.ROOT_URL + '/centers');
}
deleteCenter(id: number): Observable<{}> {
return this.http.delete(this.ROOT_URL + '/centers/' + id);
}
The get method is working and return results but it doesn't work with delete method. If it's because of CORS, then why the get method is working?
Related Questions:
“XMLHttpRequest cannot load file:///… Preflight response is not successful” error
Preflight response is not successful
Express server and Axios CORS Preflight response is not successful