0

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 when Content-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...

3

3 Answers 3

7

$http.post("ajax/testAddObject.php?answers=" + userAnswers).

Here you are sending answers as query parameters (GET) to testAddObject.php and not posting them (POST) - note the ? parameter at the end of testAddObject.php -

$_GET datas are passed as parameters in a URL. $_POST are not.

Sign up to request clarification or add additional context in comments.

4 Comments

He isnt even really using the post request it just hits the url with a parameter in the query string so it is possible to use get
That's why he gets nothing using $_POST as the parameter is not posted but added to url. You know that and I also.
Yeah, I understand that now, also. I'm kinda noob in this particular topic.. Let me just ask you, then: if I did $http.post("ajax/testAddObject.php", {answers : userAnswers}) then $_POST['answers'] would work?
I never worked with angular js but related to the angular js documentation, your code should work.
4

$_POST is populated with data from the body of the HTTP request if that data is formatted using one of the multipart or url-encoded formats.

$_GET is populated with data from the query string portion of the URL.

$_GET will be populated even if the request wasn't a GET request. It only cares if it was in the query string or not.


Since it was brought up in the comments. $_REQUEST contains the data from $_POST and the data from $_GET and the data from $_COOKIES. It's generally best avoided as it makes it possible to be surprised by data coming from places you don't expect.

2 Comments

"$_GET will be populated even if the request wasn't a GET request" You wanna say $_REQUEST no ?
@zeflex — No. I meant $_GET. $_REQUEST is different.
0

I dug a little deeper into the problem and I found this article that explains that this kinda is an AngularJS "feature", actually.

By default angular.js sends all data in json. So if you do a POST request to a PHP code then the $_POST superglobal will not be populated.

Although my code was not the completely right, if I change the client side to:

$http.post("ajax/testAddObject.php?", {answers : userAnswers}) //... 

and change the PHP accordingly, the $_POST[...] still returns nothing, which makes some sense now.

According to the same article there are two solutions, one on the client side and another on the server side

On the server you can parse input and then decode data from json:

$data = file_get_contents("php://input"); $postData = json_decode($data);` 

On the client side the data can be sent in a way PHP expects it:

$http({ url:url data : $.param(data), method : 'POST', headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} }).success(callback); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.