When using vue-axios to post data to the backend, the PHP side cannot obtain the post data. This article mainly shares with you how to solve the problem that Post cannot obtain non-form data in PHP. I hope it can help everyone.
Find the php.ini
configuration file, find the enable_post_data_reading
variable, and modify it to Open the state, comment out the prefix
; Whether PHP will read the POST data. ; This option is enabled by default. ; Most likely, you won't want to disable this option globally. It causes $_POST ; and $_FILES to always be empty; the only way you will be able to read the ; POST data will be through the php://input stream wrapper. This can be useful ; to proxy requests or to process the POST data in a memory efficient fashion. ; http://php.net/enable-post-data-reading enable_post_data_reading = On //大约656行,修改此条
After modifying the configuration, I found that it still didn't work, so I continued to check the information.
After collecting information, I found that vue-axios posts non-form data to the backend (different from Ajax). You need to use php when getting non-form data: //input
$raw = file_get_contents('php://input');//获取非表单数据 echo $raw;//输出结果
PS: When posting, the front-end request header must be set to
headers: { "Content-type": "application/json; charset=utf-8" }
Related recommendations:
php $_POST to get html Form form data tutorial_PHP tutorial
The above is the detailed content of How to solve the problem that Post cannot obtain non-form data in PHP. For more information, please follow other related articles on the PHP Chinese website!