Home >Web Front-end >JS Tutorial >How to Send JSON Data with jQuery\'s $.ajax() Method?
One common challenge when using jQuery's $.ajax() method is the conversion of JSON data to a query string when sending data to the server. This can lead to undesired results, such as array values being misinterpreted.
To resolve this issue, we need to explicitly tell jQuery to handle the data as JSON. Here's how to do it:
Serialize JSON:
Specify Content Type:
Here's an updated example:
<code class="javascript">$.ajax({ url: url, type: "POST", contentType: "application/json", data: JSON.stringify(data), complete: callback });</code>
By following these steps, you can ensure that jQuery sends your data as actual JSON instead of a query string, resolving the issue of array conversion and ensuring the integrity of your data.
The above is the detailed content of How to Send JSON Data with jQuery\'s $.ajax() Method?. For more information, please follow other related articles on the PHP Chinese website!