The following is a simple click event that uses jQuery to send an AJAX request and submit the data to the server, and display a pop-up window when the data is returned:
$(document).ready(function() {
$('button').click(function() {
// 定义要提交的数据
var data = {
name: 'John',
age: 30
};
// 发送 AJAX 请求
$.ajax({
url: 'https://example.com/api/submit',
type: 'POST',
data: data,
success: function(response) {
// 弹窗显示返回数据
alert(response);
},
error: function() {
// 如果发生错误,弹出错误提示
alert('Error');
}
});
});
});In this example, we first bind to a button click event after the document is ready. When the button is clicked, we use.ajax()The method sends a POST request to the specified URL, sending the defined data as the request body. On a successful response we use.success()The method handles the pop-up window display and pops up an alert box containing the returned data. If an error occurs we use.error()Method to handle error prompts.
Please note that this code example is for demonstration purposes only and may need to be modified based on actual needs.