I was trying to pass JSON to a PHP script in AngularJS, like so:
var testObj = {
answers : {
aa : 2,
ab : 3
}
};
var userAnswers = angular.toJson(testObj.answers);
$http.post("ajax/testAddObject.php?answers=" + userAnswers).
success(function(data){
console.log("Result: ", data);
});
On the PHP side I was doing the following:
//... new PDO connection to DB
$answers = $_POST['answers'];
//some more stuff
The $answers
variable was always empty. But then, almost randomly, I saw this question and the answer said:
$_POST
will be empty whenContent-Type: application/json
is passed in headers
And, although my code was not exactly like the one from the question, I changed the $_POST[...]
to $_GET[...]
on the PHP code and it worked! Question is, why? What is the difference between one another? Because, from what I saw, there seems to be no big difference...