0

I just created an angular 7 project

I try to send a post with some data, nothing is set in the header part

so on the server side I only get the php script called, nothing in the $_POST array

this code works fine in angular 5, I should see the data in the header log in chrome

    createPostOptions() {
        let headers = new Headers({
            'Content-Type': 'application/json',
        });

        let options = new RequestOptions({ headers: headers, withCredentials: true });
        return options;
    }

    getParts(): Observable<any> 
    {
        return this.http.post('http://localhost/site/data.php',{command:'getParts'}, this.createPostOptions())
            .pipe(map((response: Response) => {
                return this.processData(response,this.router);
            }));
    }

php code:

function cors()
{
    header("HTTP/1.1 " . "200" . " " . "OK");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");    
    header('Access-Control-Allow-Headers: Accept, Content-Type, Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods, X-Requested-With, X-API-KEY, X-Auth-Token, X-Requested-With, Authorization, Content-Range, Content-Disposition, Origin, Access-Control-Request-Method');
    header('Access-Control-Max-Age: 86400');
    header('Access-Control-Allow-Origin: '."http://localhost");
    header('Access-Control-Allow-Credentials: true');   
}

//-----------------------------------

if($_SERVER['REQUEST_METHOD']=="OPTIONS")
{
    cors();
}
else
{
    ...
}

enter image description here I should see something like this

enter image description here Any help apreciated

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
phil123456
  • 211
  • 3
  • 13
  • where are you using `createPostOptions` ? – Pardeep Jain Nov 21 '18 at 14:43
  • `'Access-Control-Allow-Origin': '*'` is nonsense. `Access-Control-Allow-Origin` is a **response** header, not a request header. – Quentin Nov 21 '18 at 15:03
  • @Quentin this was clearly overlooked by everyone, it is not a problem about php, it's about angular not sending data through the headers and is not AFAIK a duplicate...I updated the question, please kindly look at it again – phil123456 Nov 22 '18 at 12:08
  • @phil123456 — Angular is not sending the data because PHP is not responding to the preflight OPTIONS request with permission for it to send the data. Duplicate added for that other problem. – Quentin Nov 22 '18 at 12:14
  • I updated my question, forgot about the cors indeed, but still not working – phil123456 Nov 22 '18 at 12:21

1 Answers1

0

Since you are sending json in the body, but not as post parameters, you need something like

$jsonInput = file_get_contents('php://input');
$obj = json_decode($jsonInput);
$command = $obj->command;
David
  • 33,444
  • 11
  • 80
  • 118
  • that's right, completely forgot the answers where supposed to go into php://input in this case...thanks you made my day :-) – phil123456 Nov 22 '18 at 11:57
  • but it only solves php part, the issue is on angular side too ...no data is sent in the header – phil123456 Nov 22 '18 at 12:06
  • @phil123456 After your update it's a different question now... The problem was about php.... whatheader are you expecting in an OPTIONS request exactly? – David Nov 22 '18 at 12:16
  • just updated again, I forgot to copy the cors code form my angular 5 project, but although I reply with the CORS eheaders, the actual POST method is not executed by chrome...stilll digging – phil123456 Nov 22 '18 at 12:23
  • apparently Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods are not sent – phil123456 Nov 22 '18 at 12:33
  • @phil123456 Chrome usually adds the error in the console. Maybe it's because you forgot to add the port (4200) for allow-origin in your cors function – David Nov 22 '18 at 12:57
  • yeah just found it out, stupid of me, thanks it works now – phil123456 Nov 22 '18 at 13:41