gpt4 book ai didi

javascript - 是否可以使用全局 AJAX 监听器来获取 AJAX 调用的来源?

转载 作者:行者123 更新时间:2023-11-30 09:47:45 32 4
gpt4 key购买 nike

参见 this fiddle .

如果我有以下代码:

HTML :

<div class="button-wrapper">
<button id='firstButton'>
first button
</button>
<button id='secondButton'>
second button
</button>
</div>

JS :

$("button").click(function(event) {
var targetId = event.target.id;
$.ajax({
type: "GET",
url: "https://fiddle.jshell.net",
success: function(data) {
alert('target id is: ' + targetId);
}
});
});

// Is it possible for this to work the same as the above somehow?
$(document).ajaxSuccess(function(event, xhr, ajaxOptions, data) {
var targetId = event.target.id; // Event.target is `document` so does not work
alert('target id is: ' + targetId); // I want this to be the same alert as above.
});

是否可以将触发 AJAX 调用的按钮的 ID 传递给全局监听器 ($(document).ajaxSuccess)?

最佳答案

当然,将它与 ajax 选项一起传递,然后从 ajax 选项中提取它。

http://jsfiddle.net/tsmknfr2/6

$("button").click(function(e) {
var targetId = event.target.id;
$.ajax({
type: "GET",
url: "https://fiddle.jshell.net",
success: function(data) {
alert('target id is: ' + targetId);
},
foop: targetId
});
});

// Is it possible for this to work the same as the above somehow?
$(document).ajaxSuccess(function(event, xhr, ajaxOptions, data) {
var targetId = ajaxOptions.foop;
alert('target id is: ' + targetId);
});

关于javascript - 是否可以使用全局 AJAX 监听器来获取 AJAX 调用的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133549/

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