I'm having trouble getting jQuery's ajaxError global handler firing when I use a $.ajax call (on a cross domain call). Is there any way to get it to fire?
I've included a quick & dirty test (adapted from So how does $.ajaxError work?) below. Actually, none of the global ajax events fire, even if I explicitly set global to true.If I convert the $.ajax to a $('result').load it works fine, but that's not what I want.
<!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>
Thanks for any help.
Read here jQuery ajax
Some types of Ajax requests, such as JSONP and cross-domain GET requests, do not use XHR; in those cases the XMLHttpRequest and textStatus parameters passed to the callback are undefined.
Better to use curl for cross domain call
精彩评论