【发布时间】:2011-03-22 17:27:02
【问题描述】:
当我使用 $.ajax 调用(在跨域调用中)时,我无法触发 jQuery 的 ajaxError 全局处理程序。有什么办法让它着火吗?
我在下面包含了一个快速而肮脏的测试(改编自 So how does $.ajaxError work?)。实际上,即使我将 global 明确设置为 true,也不会触发任何全局 ajax 事件。如果我将 $.ajax 转换为 $('result').load 它工作正常,但这不是我想要的。
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Sandbox</title>
</head>
<body>
<div class="trigger">Trigger</div>
<div class="result"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(document).ajaxError(function (e, xhr, settings, exception) {
alert(I broke);
});
$('.trigger').click(function () {
$.ajax({
type: "GET",
global: true,
url: 'http://localhost/error',
success: function(data){
alert('ftw');
},
dataType: "jsonp",
});
});
});
</script>
</body>
</html>
感谢您的帮助。
【问题讨论】:
标签: javascript jquery ajax cross-domain