2010-11-25 69 views
24

我試圖訪問一個404事件,我可以看到404與螢火蟲回來,但錯誤函數不踢,與我的下面的代碼,我總是得到錯誤:成功?jQuery Ajax 404 Handling

即。

$.ajax({ 
    type: 'get', 
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001), 
    success: function(data, textStatus, XMLHttpRequest){ 
     console.log('Error: ' + textStatus); 
    }, 
    error:function (xhr, ajaxOptions, thrownError){ 
     alert(xhr.status); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
    } 
}); 

我再次知道1000%即時通訊404在螢火蟲沒有找到它從來沒有觸發錯誤。

我錯過了什麼嗎?

+0

我測試了完全相同的代碼和正確地得到了3個錯誤警報(第一個是'404`)。 – cambraca 2010-11-25 22:34:18

+0

你確定你實際得到了狀態碼404的響應,而不僅僅是狀態碼200的常規響​​應和包含文本的頁面這是一個404? – Guffa 2010-11-25 22:40:20

+0

@cambraca同樣在這裏。使用Firefox 3.6.12和Firebug進行測試。 – aefxx 2010-11-25 22:41:24

回答

13

還可以做什麼用$ .ajaxError功能,像這樣

$("#message").ajaxError(function(event, request, settings){ 
     $(this).show(); 
     $(this).append("<li>Error requesting page " + settings.url + "</li>"); 
    }); 
1

我有一個非常類似的結果/問題。底線是我沒有確保我的URL在ajax函數的數據部分中不包含任何相同的參數名稱。


的原因對我來說,是我生成與PHP代碼的URL來獲得一個網址

pines_url('com_referral', 'sendhttprequest') 

其輸出到一個URL參數

option=com_referral 

和參數

action=sendhttprequest 

我的BIG問題在於我還試圖在AJAX函數的數據部分發送參數「操作」。

data: {"action": "getpoints"} 

所以第二個行動是壓倒一切的是「getpoints」不存在,但棘手的部分是,螢火蟲顯然不會告訴我getpoints的URL,它會告訴我網址爲sendhttprequest。

希望沒有讓人困惑,但我也希望這可以幫助有類似問題的其他人!

1

如果在指定的時間內沒有響應,那麼添加超時值將導致錯誤回調運行。回調爲5000時,如果jsonp請求在5秒內沒有響應(即404s),那麼錯誤回調將運行。

$.ajax({ 
    type: 'get', 
    timeout: 5000, 
    url: 'url: 'https://admin.instantservice.com/resources/smartbutton/5702/10945/available.gif?' + Math.floor(Math.random()*10001), 
    success: function(data, textStatus, XMLHttpRequest){ 
     console.log('Error: ' + textStatus); 
    }, 
    error:function (xhr, ajaxOptions, thrownError){ 
     alert(xhr.status); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
    } 
}); 
0

我不知道爲什麼,但如果你使用的鏈接.fail方法,錯誤是正確捕獲即使它是一個不同的站點:

$.ajax({ 
    type: 'get', 
    url: 'http://example.com', 
    success: function(data, textStatus, XMLHttpRequest){ 
     console.log('Error: ' + textStatus); 
     } 
    }).fail(function (xhr, ajaxOptions, thrownError) { 
     alert(xhr.status); 
     alert(xhr.statusText); 
     alert(xhr.responseText); 
    });