Sending JSON Data from JavaScript to PHP
Transferring JSON data from JavaScript on the browser to a PHP server for parsing can be achieved through several methods.
Method A: Using application/json Header
request.setRequestHeader("Content-type", "application/json"); request.send(str_json);
Method B: Using application/x-www-form-urlencoded Header
Pitfall:
Attempting to send the JSON string with the application/x-www-form-urlencoded header and then accessing it via $_POST will result in an empty array, as PHP expects data in the yval=xval& format. Therefore, use php://input to access raw POST data when using the application/json header.
Additional Resources:
The above is the detailed content of How to Send JSON Data from JavaScript to PHP?. For more information, please follow other related articles on the PHP Chinese website!