Home>Article>Backend Development> What should I do if php cannot receive ajax data?
Solution to the problem that php cannot receive ajax data: 1. Add the declaration code as "Content-Type=application/x-www-form-urlencoded"; 2. Serialize it into a string according to the format agreed by both parties.
Recommended: "PHP Tutorial"
Solution to the problem that PHP cannot receive the json value submitted by ajax Method
JS needs to specify contentType = application/x-www-form-urlencoded, so that the sent json data will be received by PHP's $_POST.
The default value of contentType in jQuery: "application/x-www-form-urlencoded" (content encoding type when sending information to the server). The default value is suitable for most cases, if you explicitly pass a content-type to $.ajax() then it will definitely be sent to the server (even if there is no data to send).
1. If you use the post method to send, there must be a statement of Content-Type = application/x-www-form-urlencoded, otherwise php will not parse the incoming data into the $_POST array.
declares a contentType = application/json, Ajax automatically adds a Content-Type = application/x-www-form-urlencoded declaration. These two statements will also make php confused.
2. The data sent is {data:jsonData}, which is a js object. Even if it can be sent successfully, PHP will not be able to parse it because it does not recognize the object. All data structures exchanged with the outside world need to be serialized into strings in the format agreed by both parties
Assume that your Js.Ajax can convert js objects into JSON format strings
And you The declaration of contentType = application/json is also removed
Then you can see the element with that JSON string as the key in $_POST
If you insist on not wanting to remove contentType = application/json The statement
Perhaps you can use file_get_contents('php://input') to get the incoming content
php://input allows reading the original data of POST. It puts less pressure on memory than $HTTP_RAW_POST_DATA and does not require any special php.ini settings. php://input cannot be used with enctype=”multipart/form-data”.
The above is the detailed content of What should I do if php cannot receive ajax data?. For more information, please follow other related articles on the PHP Chinese website!