2016-10-21 196 views
0
忽略

我面對小問題,我的Web API返回我304「未修改」響應時,有在數據沒有變化的更新被髮送,和我的應用程序實現的錯誤頁面,它將我重定向到錯誤頁面。我希望這不會出現錯誤,而應該忽略。的Web API 304「未修改」響應錯誤如何在Angularjs

好心幫我怎麼不理使用HTTP攔截此響應。下面是編寫的代碼,它可以攔截HTTP響應,然後重定向我根據錯誤的特定頁面,我需要忽略此304響應:

myApp.factory('httpErrorResponseInterceptor', ['$q', '$location', 
    function ($q, $location) { 
     return { 
      response: function (responseData) { 
       return responseData; 
      },    

      responseError: function error(response) { 

       switch (response.status) { 
        case 401:       
         $location.path(baseUrl + 'Account/Login'); 
         break; 
        case 403: 
         $location.path(baseUrl + '403'); 
         break; 
        case 404: 
         $location.path(baseUrl + '404'); 
         break; 
        case 405: 
         $location.path(baseUrl + '405'); 
         break; 
        case 500: 
         $location.path(baseUrl + '500'); 
         break; 
        case 503: 
         $location.path(baseUrl + '503'); 
         break; 
        case 408: 
         $location.path(baseUrl + 'timeout'); 
         break; 
        default: 
         $location.path(baseUrl + 'error'); 
       } 

       return $q.reject(response); 
      } 
     }; 
} 
]); 
+0

向我們展示你的「應用程序已實施的錯誤頁面,其重定向我的錯誤頁面」 ..基本上你可以捕捉並檢查304狀態,並且如果其他人忽略該狀態。 – cjmling

+0

HTTP攔截器,我使用,我要在這裏忽略響應。 – usman

回答

0

我已經找到了解決方案忽略304「未修改」錯誤的響應編寫這些代碼:

//ignore 304-'Not Modified' response 
if (response.status === 304) { 
    return $q.resolve(response); 
} 

和完整的解決方案將是如下:

smartBizzApp.factory('httpErrorResponseInterceptor', ['$q', '$location', 
    function ($q, $location) { 
     return { 
      response: function (responseData) { 
       return responseData; 
      },    

      responseError: function error(response) { 

       //ignore 304-'Not Modified' response 
       if (response.status === 304) { 
        return $q.resolve(response); 
       } 

       switch (response.status) { 
        case 401:       
         $location.path(baseUrl + 'Account/Login'); 
         break; 
        case 403: 
         $location.path(baseUrl + '403'); 
         break; 
        case 404: 
         $location.path(baseUrl + '404'); 
         break; 
        case 405: 
         $location.path(baseUrl + '405'); 
         break; 
        case 500: 
         $location.path(baseUrl + '500'); 
         break; 
        case 503: 
         $location.path(baseUrl + '503'); 
         break; 
        case 408: 
         $location.path(baseUrl + 'timeout'); 
         break; 
        default: 
         $location.path(baseUrl + 'error'); 
       } 

       return $q.reject(response); 
      } 
     }; 
} 
]); 
相關問題