2
我在使用$ .ajax调用(在跨域调用时)时遇到了jQuery的ajaxError全局处理程序触发问题。有什么办法让它着火吗?
我在下面包含了一个快速的&脏测试(改编自So how does $.ajaxError work?)。实际上,全局ajax事件都不会触发,即使我明确地将全局设置为true。如果我将$ .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>
感谢您的任何帮助。
哎呀,这是因为我是个白痴。没有阅读全球处理程序文档中斜体显示的位:“注意:不管全局值如何,全局事件都不会因跨域脚本或JSONP请求而被解雇。” – dochead 2011-03-22 12:43:56