0

I've been debugging this for hours. Tried to set the header etc but no luck!

My controller

 $http({
    url: 'http://myphp.php/api.php',
    method: "POST",
    data: {'wtf':'test'}
})
.then(function(response) {
        console.log(response);
    }, 
    function(response) { // optional
        // failed
    }
);

and my php

<?php
echo "test"; 
echo $_POST["wtf"];
?>

In my network tab this is how it look like

enter image description here

Not sure what's wrong man, really exhausted, I'm stuck for hours! Why my $_POST['wtf] didn't echo?

James Lemon
  • 426
  • 5
  • 17

2 Answers2

0

$http is serializing the data to JSON in the request body but PHP's $_POST is looking for key/values parsed from posted form data. These are two different mechanisms for posting data so you need to choose one and use that mechanism on both sides.

You have two options to solve this:

  1. In your PHP code, parse the request body as JSON data and then use that object to retrieve your data. See this StackOverflow question for more information.

  2. Modify your $http request to post the data as form data. See this StackOverflow question for more information.

Community
  • 1
  • 1
David Anderson
  • 8,306
  • 2
  • 27
  • 27
0

$http.post (by default) does not send the data as key/value pairs. It sends it as post request.

Therefore, in your php script you should consume it like this:

$input = file_get_contents('php://input');

And then parse it like this:

$data = json_decode($input, true);
o4ohel
  • 1,779
  • 13
  • 12