2013-02-18 117 views
1

我不知道如何處理$ .post()中的404錯誤?如何處理jquery.post中的404錯誤

$.post(url, data, function(returnedData) { 

它只能處理成功的數據,但我要處理的404錯誤也in.i知道如何用Ajax做,但不知道有此功能,請幫助。

function returnData(url,data,type){ 
     $.post(url, data, function(returnedData) { 
     if(type == "contacts") 
     { 
     ko.applyBindings(new ContactsViewModel(returnedData,"#KnockOutContacts",url,data),$("#KnockOutContacts")[0]); 
     ko.applyBindings(new ContactsViewModel(returnedData,"#ContactDetails",url,data),$("#ContactDetails")[0]); 
     } 
     else if(type == "logs") 
     { 
     alert(returnedData); 
     alert(1); 
     ko.applyBindings(new LogsViewModel(returnedData,url,data),$("#KnockOutLogs")[0]); 
     } 
     else if(type == "sms") 
     { 
      ko.applyBindings(new SmsViewModel(returnedData,"#KnockOutSmsData",url,data),$("#KnockOutSmsData")[0]); 
      ko.applyBindings(new SmsViewModel(returnedData,"#KnockOutSms",url,data),$("#KnockOutSms")[0]); 
     } 
     else if(type == "calendar") 
     { 
     ko.applyBindings(new CalendarViewModel(returnedData,"#KnockOutCalendar",url,data),$("#KnockOutCalendar")[0]); 
     } 
     else if(type == "search") 
     { 
     ko.applyBindings(new SearchViewModel(returnedData,"#searchbox",url,data),$("#searchbox")[0]); 
     } 
     else if(type == "location") 
     { 
     ko.applyBindings(new LocationViewModel(returnedData,"#KnockOutMaps",url,data),$("#KnockOutMaps")[0]); 
     } 
     else if(type == "photos") 
     { 
     ko.applyBindings(new PhotosViewModel(returnedData,"#photogallary",url,data),$("#photogallary")[0]); 
     ko.applyBindings(new PhotosViewModel(returnedData,"#PhotosDown",url,data),$("#PhotosDown")[0]); 
     } 
    }); 
} 

我bascially申請綁定,當我得到的數據,但是當我沒有得到的數據它沒有走成功函數內部和多數民衆贊成打破我的JS。

回答

3

您可以使用全局錯誤處理程序:

$(document).ajaxError(function(e, xhr, settings, exception) { 

}); 
7

閱讀statusCode回調here

$.ajax({ 
    url: "/page.htm", 
    type: "POST", 
    statusCode: { 
     404: function() { 
      alert("page not found"); 
     } 
    } 
}) 

編輯。

還可以與$.post

$.post(url, data, function(returnedData) { 
    //callback 
}).fail(function(jqXHR, textStatus, errorThrown){ 
    if(jqXHR.status == 404) { 

    } 
}); 
+0

accieved我不想用'$ .ajax' – rohitarora 2013-02-18 10:42:00

+0

您可以使用'.fail'回調做到這一點。請參閱編輯。 – 2013-02-18 10:52:59

+0

失敗dint曾爲我工作 – rohitarora 2013-02-18 11:00:46