我的项目中存在CORS问题。我从Angular 8开始:
httpOptions = {
headers: new HttpHeaders({
'Content-Type':'application/json',
'Authorization':'Basic bm92Z*********************'
})
};
而且我确实以这种方式进行操作:
public getStudents() : Observable<IStudent[]>{
return this.http.get<IStudent[]>(this.writeUrl, this.httpOptions)...// this.writeUrl is the request url
}
在使用Java的服务器中,我这样做:
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers",
"origin, content-type, accept, authorization");
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
它给了我CORS,但我不知道为什么,有人可以帮助我吗?
Angular服务器在http://localhost:4200/上运行,而Spring Boot服务器在http://localhost:8080/上运行。