0

I am developing mobile application for one of my webapp whose all the data resides on server. On post request data will be sent in response in JSON format. I am doing a post request as follows:

$http.defaults.headers.post['Content-Type'] ='application/x-www-form-urlencoded';
$http.post(base_url+"get/memberLogin.php", {'username':userName, 'password':passWord, 'tempKey':'XHJJUQWERgfrbbbbokaw1222344'}...

the data is getting sent in post request as that of i can see in the firebug of firefox browser. But at server side when i do var_dump($_POST) or var_dump($_REQUEST) i am getting the empty array. How is it so??? As i am posting data on server, it should be captured using $_POST but didnt work

Instead if i send data in following format:

$http.post(base_url+"get/memberLogin.php?username="+userName+"&password="+passWord, {}...

I am getting parameter values at server side using $_REQUEST. What is the problem with $_POST??

Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Shaggie
  • 1,799
  • 7
  • 34
  • 63

1 Answers1

1

But at server side when i do var_dump($_POST) or var_dump($_REQUEST) i am getting the empty array.

While you are setting the content-type to application/x-www-form-urlencoded, Angular is encoding the data as JSON.

PHP fails to parse it (because it isn't application/x-www-form-urlencoded) so it has no data to populate $_POST wiht.

I am getting parameter values at server side using $_REQUEST. What is the problem with $_POST??

PHP has stupid naming conventions.

$_GET does not contain all data from a GET request. It contains data from a query string.

$_POST does not contain all data from a POST request. It contains data from the request body.

This:

$http.post(base_url+"get/memberLogin.php?username="+userName+"&password="+passWord

… makes a POST request, but you are putting all the data in the query string (where you should be passing it through encodeURIComponent first).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • if i change content type to application/json then even it doesn't reveive at server side – Shaggie May 22 '15 at 09:02
  • You can either change the client to send form encoded data or you can remove the lie that you are sending form encoded data and parse the JSON on the server, which you will have to [do yourself](http://stackoverflow.com/questions/19004783/reading-json-post-using-php) because PHP has no native handling of JSON encoded HTTP requests. – Quentin May 22 '15 at 09:12
  • well read my question content carefully. On successful request handling server will send the response in JSON format. I am doing that on server side with header('Content-Type: application/json'); i m completely blank of what you are arguing here!!! – Shaggie May 22 '15 at 09:30
  • I'm talking about the **request** not the response. – Quentin May 22 '15 at 10:02
  • My concern is what should i do to get the parameters values in the post request using $_POST at server side script... forget about rest all... – Shaggie May 22 '15 at 10:18
  • Then change the client to send form encoded data (which I guess you'll have to put together manually) – Quentin May 22 '15 at 10:21
  • You change your JavaScript to make it send form encoded data instead of JSON encoded data. Angular's $http *might* have a way to do that (check the docs) or you could encode the form data manually. – Quentin May 22 '15 at 10:36