Home >Backend Development >PHP Tutorial >How to Access Axios POST Parameters in PHP Using Different Methods?
Axios POST Parameters Not Accessible in PHP
In your code, you're sending a POST request using Axios with the "application/x-www-form-urlencoded" content type. However, you're encountering an issue where the POST data is not being populated into the $_POST or $_REQUEST arrays on the PHP side.
The root cause of this problem lies in the format of the POST data. PHP expects POST data to be in the "application/x-www-form-urlencoded" format, which is the default format for HTML form submissions. By default, Axios serializes JavaScript objects to JSON, which is not a supported data format for $_POST.
Solution: Using URLSearchParams or qs Library
To resolve this issue, you can use the following methods to encode the POST data in the correct format:
<code class="javascript">const params = new URLSearchParams(); params.append('param1', 'value1'); params.append('param2', 'value2'); axios.post('/foo', params);</code>
<code class="javascript">const qs = require('qs'); axios.post('/foo', qs.stringify({ 'bar': 123 }));</code>
Alternatively, you can modify PHP to handle JSON as POST data:
Refer to this answer for details on how to configure PHP to handle JSON as a data format for populating $_POST.
The above is the detailed content of How to Access Axios POST Parameters in PHP Using Different Methods?. For more information, please follow other related articles on the PHP Chinese website!