试图向我的服务器上的HTML页面发送一个response.sendRedirect,但是角边的函数总是试图将我的HTML页面解析为JSON。尽管我正在将我的可观察到的泛型配置为“任意”。到处找都找不到答案。谢谢所有想帮忙的人。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().invalidate();
String loginUrl = new Gson().toJson("login.html");
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(loginUrl);
out.flush();
}
public logOut():Observable<any> {
return this.client.get<any>("../Login");
}
public disconnect():void {
this.service.logOut().subscribe( content => {
}, fail => {
console.log(fail);
});JSON.parse位置0处JSON中的意外令牌<
http://localhost:8080/解析过程中Http失败.
发布于 2019-07-29 01:43:34
Daniel :不要将login.html从服务器上发回。发回这样的东西:'{"redirectTo": "login"}'。
首先,您需要在服务中导入角路由器:
import {Route} from "@angular/router"然后将其插入组件构造函数中:
constructor(private route: Route) { }最后,在您的disconnect()方法中:
public disconnect():void {
this.service.logOut().subscribe( content => {
this.route.navigate([`/${content.redirectTo}`])
}, fail => console.log('Display fail error:', fail)
)
);此外,disconnect()方法可能不是必要的,因为您可以在以下几个方面完成工作:
logOut(): Observable<any> {
return this.client.get<any>('../Login')
.pipe(
map(success => this.route.navigate([`/${success}`]),
catchError(fail => console.log('Display fail error:', fail)
// and then redirect somewhere
)
)
}https://stackoverflow.com/questions/57245617
复制相似问题