gpt4 book ai didi

jquery - 在jQuery中处理多个AJAX调用

转载 作者:行者123 更新时间:2023-12-03 08:11:25 24 4
gpt4 key购买 nike

我的网站上有一个标签结构。

在每个选项卡上单击时,将调用带有唯一URL的AJAX。
但是,每当单击连续的选项卡并触发ajax时,调用都会出错而不是成功。

仅加载时加载的选项卡正在获取正确的输出。
在切换选项卡上(如果第一个选项卡的URL正在获取大量数据),它将出错。

我还使用了.abort(),但我认为它没有用。
我可能想念一些东西。谁能提出任何解决方案。这是我的示例代码:

$(document).ready(function () {
xhr = $.ajax({
url: "www.something.com",
type: "GET",
cache: true,
success: function (response) {
alert("successful");
},
error: function (e) {
alert("Oops Something went wrong");
}
});

});

$("#stickertab").click(function (e) {
appUrl = "www.nothing.com";

$("#show_Sticker").empty();
if (xhr != null) xhr.abort();
xhr = $.ajax({
url: appUrl,
type: "GET",
cache: true,
success: function (response) {
alert("successful");
},
error: function (e) {
alert(" Oops Something went wrong");
}
});

});

这是我得到的错误:
e = Object {readyState: 0, status: 0, statusText: "abort"}

最佳答案

e.preventDefault()添加到您的函数中。

$("#stickertab").click(function (e) {

e.preventDefault(); // <-- here

appUrl = "www.nothing.com";

$("#show_Sticker").empty();
if (xhr != null) xhr.abort();
xhr = $.ajax({
url: appUrl,
type: "GET",
cache: true,
success: function (response) {
alert("successful");
},
error: function (e) {
alert(" Oops Something went wrong");
}
});

});

关于jquery - 在jQuery中处理多个AJAX调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com