I have tried literally everything shown as solution. I have tried every option in Angular2 http POST body sent as null and many other related posts, nothing has worked so far for me. I have the main react app deployed with npm start and part of the login is as follows:
export async function loginService(credentials) {
return fetch('http://localhost:3333/mock-server/index.php', {
method: 'POST',
body: JSON.stringify(credentials) //user:"user"
})
.then(
function(data){
if(data.status === 422){
data.json().then(val=>console.log("DEVOLUCION 422: "+JSON.stringify(val)));
return;
}
return data.json();
}
).catch(error=>{
console.error('error! ->', error);
})
}
PHP File:
<?php
$headers = apache_request_headers();
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header("HTTP/1.1 200 OK");
die;
}
$username = $_POST['user'];
This $username
returns null. I don't know what else to do, I have tried sending a plain 'user=user' as part of the url as well, but always, it returns null.
Worth mentioning my php server is the Vscode extension "php server". I tried directly as well directly from php directory executable, with the same luck. When I try with Postman, it returns the name correctly.