我有一个帖子调用,应该将一些内容发布到URL,在重定向发生之后,我看到重定向URL并在控制台 - >网络中获取正确的数据,但是页面没有被重定向。
试图访问响应的标头,它在“位置”中指定重定向网址,但也无法访问标头。
位置/ abs /重定向?clientId = abc
我的重定向网址应该是来自我的帖子的响应标头中上面提到的位置键的值。
这是我的帖子代码。
const test= this.http.post(url, JSON.stringify({ Req: req
})).subscribe(
data => {
console.log(data);
}
);
任何人都可以帮我如何从Post访问响应的标题,或直接允许重定向。我正在使用角6。
正如@varundhariyal指出的那样,路由器不会像外部链接那样工作。解决方法是使用:window.location.href= yourURL
如果您想要的URL在响应中,那么您可以尝试这样的事情:
在构造函数中定义一个Router实例:
constructor(private route: Router)
(你需要导入它:import { Router } from '@angular/router';
)
并使用它导航到订阅响应中的所需URL:
const test= this.http.post(url, JSON.stringify({ Req: req })).subscribe(
data => {
this.route.navigate(data.url); //Only works for routes defined in the RouterModule
window.location.href = data.url; //Absolute path to any URL
}
);