// 保存 xhr 对象
let xhr = null;
function sendRequest() {
xhr = $.ajax({
url: '/api/data',
type: 'GET',
success: function(response) {
console.log('请求成功:', response);
},
error: function(xhr, status, error) {
if (status === 'abort') {
console.log('请求已被取消');
} else {
console.log('请求错误:', error);
}
}
});
}
// 取消请求
function cancelRequest() {
if (xhr) {
xhr.abort();
xhr = null;
console.log('已发送取消请求');
}
}
$('.test').click(function(){
cancelRequest();
});
1050

被折叠的 条评论
为什么被折叠?



