I'm trying to do a very simple post http request from Angular2 to a PHP file in localhost// and I'm getting CORS error:
The PHP code:
<?php
header("Access-Control-Allow-Origin: http://localhost/4200");
header("Access-Control-Allow-Credentials: true ");
header("Content-Type: application/x-www-form-urlencoded");
header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
$post = file_get_contents('php://input');
echo($post);
?>
Angular2 http post code in the upload component, inside a upload() function:
upload() {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
var params = 'jfjbdjskfbsdjkfs';
this.http.post('http://localhost/uploadform/prueba.php', params , {headers: headers})
.subscribe(
data => console.log('Received:' + data),
err => console.log(err),
() => console.log('Call Complete')
);
}
The original post that I've trying to do is sending a file to the php and uploading that file to some path in my computer, but since I've been having CORS issues I tried with a simple string as "params", and I'm still having that problem...
I had enabled CORS inside the PHP file in different ways but I'm still not getting any positive result. I've been with this for 3 days now, and it is really frustrating.