2017-05-09 58 views
0

我正在使用Http连接到服务器。Angular2是否可以使用http错误消息?

服务器通过给我403 forbidden error自动阻止未经授权的用户。

虽然我不需要使用AuthGuard(coz服务器正在为我做),但是我仍然希望在返回403禁止错误时显示一些消息,而不是仅显示空白页面。

所以我的问题是..是否有可能在Angular2中使用这个禁止的403错误,这样我可以在错误返回时显示一些东西?

if (errorMessage == 403) { 
showModal() } 

也许这样吗?

+0

这是可能的,因为你应该得到来自服务器的响应。您需要创建一个403错误的HTML页面并将其显示为 –

+0

,并且可以告诉我该怎么做?像为403错误的HTML页面创建另一个组件? – Lea

回答

1

的simples例子只是

@Inject export default class SomeService { 
    constructor(readonly http: Http) {} 

    getData() { 
    return this.http.get('api/path-to-something') 
     .catch(error => { 
     if (error.status === 403) { 
      // Just using alert for simplicity's sake. 
      alert('You don't have sufficient access to do that!'); 
     } 
     throw error; 
     }) 
     .map(response => response.json()); 
    } 
} 
相关问题