I'm trying to call a web service from angular 5 to PHP.
I'm using POST method but I have trouble retrieving data in PHP side.
Send data is shown in payload but it's not retrieved in php side.
Angular service.ts
this.URL = "http://localhost/angular/WEBSERVICE_PHP/test.php";
const headers = new Headers();
headers.append('Content-Type', 'application/json');
let data = 'visitor_id=55';
return this.http
.post(this.URL,JSON.stringify(data),{
headers: headers
})
.map( Response => console.log(Response) );
PHP PAGE
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: POST, GET");
header('P3P: CP="CAO PSA OUR"'); // Makes IE to support cookies
header('content-type: text/plain');
header("content-type: application/x-www-form-urlencoded");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-
Requested-With");
header("Access-Control-Max-Age: 172800");
if(isset($_POST))
{
// $post = 'test data'; // This data get sent
// return $post;
echo $post = $_POST['visitor_id'];
return $post;
}
file_get_contents('php://input')since you are sending JSONJSON.stringifyondata? The waydatais defined it looks like it should beJSON.parseinstead.var_dump($_POST);It does not contain what you think it does. Trylet data = {visitor_id: 55};and.post(this.URL,data,{, and remove the json header instead. It should be able to read the data without having to use json. If you do want to use json, don't forget to decode it in PHP before you try to access anything.