2017-08-06 51 views
1

我有以下代碼:JQuery的 - 默認失敗()函數對Ajax

 

    $.get('somescript.php', callback_fn).fail(function(){ alert("Error message!"); }); 
    $.get('somescript2.php', callback_fn2).fail(function(){ alert("Error message!"); }); 
    $.get('somescript3.php', callback_fn3).fail(function(){ alert("Error message!"); }); 

的失敗功能總是相同的。

我想這樣做,仍然顯示失敗錯誤,這有可能嗎?

 

    $.get('somescript.php', callback_fn); 
    $.get('somescript2.php', callback_fn); 
    $.get('somescript3.php', callback_fn); 

回答

1

您可以使用$(document).ajaxError作爲解決方案。

// Requests 
 
$.get('somescript.php', callback_fn); 
 
$.get('somescript2.php', callback_fn); 
 
$.get('somescript3.php', callback_fn); 
 

 
// Mock function 
 
var callback_fn = function() { 
 
    // does nothing yet 
 
} 
 

 
// This is the key to the solution 
 
$(document).ajaxError(function(){ 
 
    alert('error'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

試試這個代碼:

$.getJSON('somescript.php') 
 
     .done(function() { alert('request successful'); }) 
 
     .fail(function() { alert('Error message!'); }); 
 

 
$.getJSON('somescript2.php') 
 
     .done(function() { alert('request successful'); }) 
 
     .fail(function() { alert('Error message!'); }); 
 
     
 
$.getJSON('somescript3.php') 
 
     .done(function() { alert('request successful'); }) 
 
     .fail(function() { alert('Error message!'); }); 
 
    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>