Home  >  Article  >  Backend Development  >  Post cannot get form data

Post cannot get form data

php中世界最好的语言
php中世界最好的语言Original
2018-03-24 15:53:232919browse

This time I will bring you Post’s inability to obtain form data. What are the precautions to solve Post’s inability to obtain form data? The following is a practical case, let’s take a look.

Problem description

When using vue-axios to post data to the backend, the PHP side cannot obtain the posted data.

Problem Solving

Modify php.ini configuration

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.

Getting non-form data

After collecting information, I found that vue-axios posts non-form data to the backend (different from Ajax). You need to use php://input when getting non-form data.

$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"
}
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

php custom two-dimensional array sorting function array

implementation of phpunit interface automated testing function

The above is the detailed content of Post cannot get form data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn