Cordova: Can not get the request parameters values at server side using $_POST

我只是一个虾纸丫 提交于 2020-01-06 14:52:28

问题


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??


回答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).



来源:https://stackoverflow.com/questions/30391569/cordova-can-not-get-the-request-parameters-values-at-server-side-using-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!