Reputation: 584
in my controller.js I do like this
$http({
url: 'http://example.com/api.php',
method: "POST",
data: { 'YTParam' : 'test' }
})
.then(function(response) {
console.log(response.data);
},
function(response) { // optional
// failed
}
);
I even checked the network tool, the param did passed.
and in my api.php I try echo $_POST["YTParam"]
but in my console I see nothing? What's wrong here? I tried to echo "string", it did appeared..
Upvotes: 1
Views: 105
Reputation: 10698
Your data is not in the $_POST
array, because your angular JS contentType is set to application/json
by default, and PHP expects application/x-www-form-urlencoded
for a POST request. Quoting from docs :
$httpProvider.defaults.headers.post: (header defaults for POST requests)
Content-Type: application/json
To change this, either change this value in your config :
yourApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
}]);
Or at the time you send your request :
$http({
url: 'http://myURL.com/api/',
method: "POST",
headers: {
'Content-Type': "application/x-www-form-urlencoded"
},
data: { 'YTParam' : 'test' }
})
You can set contentType
directly as a property, but I'm not 100% sure of this.
Upvotes: 0
Reputation: 28750
Angular is not setting a header your server handles, try using this:
$_POST = json_decode(file_get_contents("php://input"), true);
Upvotes: 1