Understanding JSON Data Transmission with jQuery
Sending data in JSON format is crucial for efficient communication between web pages and servers. However, if you encounter data being sent in an unformatted manner, like "City=Moscow&Age=25," it may be due to the lack of proper request configuration.
The provided code attempts to send JSON data using jQuery's $.ajax() method. By default, jQuery converts data to a query string, resulting in the "City=Moscow&Age=25" format. To resolve this, follow these steps:
Here is the corrected code:
var arr = { City: 'Moscow', Age: 25 };
$.ajax({
url: 'Ajax.ashx',
type: 'POST',
data: JSON.stringify(arr),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) {
alert(msg);
}
});
Additional Notes:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3