Home >Backend Development >PHP Tutorial >How to Send JSON Data from JavaScript to PHP: Two Effective Approaches?
Sending JSON Data from Javascript to PHP
The challenge arises when attempting to relay JSON data from a browser's Javascript environment to a server where PHP will interpret it. To address this, two approaches have proven effective:
Version 1: Leveraging application/json Request Header
Version 2: Employing application/x-www-form-urlencoded
Pitfall to Avoid
Initially, attempting to convey JSON strings with the incorrect request header can lead to difficulties. Using application/x-www-form-urlencoded with the $_POST array in PHP will result in an empty array. Conversely, utilizing application/json requires accessing raw POST data via php://input.
References
The above is the detailed content of How to Send JSON Data from JavaScript to PHP: Two Effective Approaches?. For more information, please follow other related articles on the PHP Chinese website!