Angular2在获取资源时监视302重定向 [英] Angular2 watch for 302 redirect when fetching resource
问题描述
我需要从我公司拥有的另一个域中提取一些资源.我想通过GET请求提取受保护的HTML内容.
I have a requirement to pull a few resources from another domain held by my company. I want to pull secured HTML content with GET requests.
当用户从应用程序中注销时,请求的内容将返回302到登录页面.
When a user is signed out of the application the requested content will return a 302 to the login page.
我尝试嗅探302标头的尝试并没有返回到目前为止我希望的结果.我的Observable返回的响应是200(登录页面).
My attempts to sniff the headers for a 302 haven't returned what I'd hoped for so far. The response returned by my Observable is a 200 (login page).
这是我的示例应用程序.
Here is my sample app.
export class MenuComponent implements OnInit {
private _resourceUrl = "http://localhost:3001/resources/menu";
constructor(private _http: Http){
}
menu: string;
ngOnInit(): void {
this.getMenu()
.subscribe(
response => {
console.log(`Response status: ${response.status}`);
this.menu = response.text();
},
error => console.log(<any>error));
}
getMenu(): Observable<Response>{
return this._http.get(this._resourceUrl)
.map((response: Response) => response)
.catch(this.handleError);
}
private handleError(error: Response){
console.log(error);
return Observable.throw(error.json().error || 'Server Error');
}
}
我什至在正确的轨道上吗?
Am I even on the right track?
推荐答案
如果服务器发送带有302状态代码的重定向,并且该URL带有要在Location
标头中进行重定向的URL,则该重定向将由浏览器自动处理,即对此URL的请求已执行.
If the server sends a redirect with a 302 status code with the URL to redirect within a Location
header, the redirect is automatically handled by the browser, i.e. a request to this URL is executed.
这就是为什么XHR(及其周围的Angular2包装器,即Http
类)看不到第一个请求的结果,而只能看到第二个请求的响应的原因.
That's why XHR (and the Angular2 wrapper around it, i.e. the Http
class) won't see the result of the first request but only the response of the second one.
这篇关于Angular2在获取资源时监视302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!