试图向我的服务器上的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:28:00
免责声明:我不是JAVA开发人员,所以可能有一种更有效的方法来执行JAVA后端。
好的,为了让它正常工作,您应该更新代码如下:
后端:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().invalidate();
String loginUrl = "index.html";
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(loginUrl);
out.flush();
}角:
public logOut():Observable<string> {
return this.client.get<string>("../Login");
}
public disconnect():void {
this.service.logOut().subscribe(redirectUrl => {
const baseUrl = window.location.href.split('/').slice(0,4).join('/');
window.location.href = `${baseUrl}/${redirectUrl}`;
}, fail => {
// Do fail stuff
});
}更新:根据注释更改重定向位置。
海事组织这个解决方案开始变得有点脏了。它将在紧急情况下工作,但从长远来看,我建议改为与角路由相关的解决方案,类似于“Thomas Cayne”发布的解决方案。
发布于 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
复制相似问题