飞行前的响应在angular2/4中无效(重定向)
我使用angular4作为前端,springboot应用程序作为后端。前端和BFF(前端后端)之间的集成方法是RESTful请求 因此前端向SpringBoot REST控制器处理的后端端点发送POST请求,后端向前端返回302重定向响应 问题是我一直遇到XMLHttpRequest无法加载。飞行前的响应无效(重定向)问题 不知何故,我不认为这是服务器(后端)的问题,因为我使用postman测试后端,它确实转到重定向网站(postman没有呈现重定向网站tho,它确实收到了网站文件) 来自angular4前端的POST请求 angular4服务文件中的可观测函数 } 来自springboot应用程序后端的响应头代码 后端Restcontroller 后端响应头 网络日志截图:飞行前的响应在angular2/4中无效(重定向),angular,rest,redirect,spring-boot,rxjs,Angular,Rest,Redirect,Spring Boot,Rxjs,我使用angular4作为前端,springboot应用程序作为后端。前端和BFF(前端后端)之间的集成方法是RESTful请求 因此前端向SpringBoot REST控制器处理的后端端点发送POST请求,后端向前端返回302重定向响应 问题是我一直遇到XMLHttpRequest无法加载。飞行前的响应无效(重定向)问题 不知何故,我不认为这是服务器(后端)的问题,因为我使用postman测试后端,它确实转到重定向网站(postman没有呈现重定向网站tho,它确实收到了网站文件) 来自ang
任何帮助都将不胜感激,谢谢 如果我没看错这个问题,你似乎在做:
- 让服务器响应所有缺少401身份验证的API请求
- 在服务器(/login)上有一个端点,它以302响应google
- 在前端,监听401,如果检测到,将窗口位置更改为/login
validateData(id, pw, path): Observable<Object>{
let url:string= "http://localhost:8080/backend";
let headers = new Headers({'Accepts':'text/plain ; application/json', 'Content-type':'Application/json; charset=utf-8', 'Access-Control-Allow-Origin':'*'});
let userData = JSON.stringify({username: id, password: pw, resumePath: path});
let options = new RequestOptions({headers: headers});
//return response
return this.http.post(url, userData ,options).map(res=>res.json());
}
validateData(id:string, pw:string, path:string){
return this.loginService.validateData(id, pw, path).subscribe(
//BFF return an object contains REF //implements interface on response
response=> {
console.log(response);
},
error=> {
console.log("error occurs");
console.log(error);
},
()=> {}
);
@CrossOrigin(origins = "http://localhost:4200") //enable CORS && temperory origin url
@RequestMapping(value= "/backend", method= RequestMethod.POST, produces= "text/plain")
public ResponseEntity getMockData (@RequestBody Credential credential) {
System.out.println("username: "+ credential.getUsername()+" password: " +credential.getPassword()+" resumePath:" + credential.getResumePath());
return this.postAuthCred(credential.getUsername(), credential.getPassword(), credential.getResumePath());
}
private ResponseEntity postAuthCred(String username, String password, String path){
HttpHeaders responseHeaders = new HttpHeaders();
String responseBody= "http://www.google.ca";
responseHeaders.add("Origin", "http://localhost:8080");
responseHeaders.add("Access-Control-Allow-Origin", "*");
responseHeaders.add("Accept", "text/plain;application/json");
responseHeaders.add("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition, Content-Description");
responseHeaders.add("Content-Type", "text/plain");
responseHeaders.add("Access-Control-Allow-Methods ", "HEAD, GET, POST");
responseHeaders.add("Access-Control-Max-Age", "1728000");
responseHeaders.add("Location", "https://www.google.ca/");
return new ResponseEntity(responseBody, responseHeaders, HttpStatus.FOUND);
}