1

$.post() で 404 エラーを処理する方法がわかりません??

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

成功データのみを処理できますが、404 エラーも処理したいです。

    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]); 
        }
    });
}

私は基本的にデータを取得するときにバインディングを適用しますが、データを取得しないときは成功関数内に入らず、それが私のjsを壊しています。

4

2 に答える 2

9

statusCodeコールバックについてはこちら

$.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) {

    }
});
于 2013-02-18T10:41:31.747 に答える
3

グローバル エラー ハンドラを使用できます。

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

});
于 2013-02-18T10:41:12.090 に答える