【发布时间】:2019-11-12 20:29:45
【问题描述】:
我想通过 Angular 使用 POST 方法注销,这是我的代码:
logout() {
const url = 'http://localhost:8181/user/logout';
const xToken = localStorage.getItem('xAuthToken');
const basicHeader = 'Basic ' + localStorage.getItem('credentials');
const headers = new Headers({
'x-auth-token': xToken,
'Authorization': basicHeader
});
// return this.http.get(url, { headers: headers }); // This will work
return this.http.post(url, { headers: headers }); // This will generate error
}
这是我的后端:
@RequestMapping("/user/logout")
public ResponseEntity<String> logout(){
SecurityContextHolder.clearContext();
return new ResponseEntity<String>("Logout Successfully!", HttpStatus.OK);
}
奇怪的是上面的代码在this.http.get 下工作,但在this.http.post 下会产生下面的错误。这是this.http.post 的错误:
POST http://localhost:8181/user/logout 401
如果我使用 HttpClient 修改代码,像这样:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor(private http: HttpClient) {
}
sendCredential(username: string, password: string) {
let url = "http://localhost:8181/token";
let encodedCredentials = btoa(username + ":" + password);// encode in base64 to send a token
let basicHeader = "Basic " + encodedCredentials;
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': basicHeader
})
// send credential method when login component
return this.http.get(url, { headers: headers }); // Error at this line
}
checkSession() {
const url = 'http://localhost:8181/checkSession';
const xToken = localStorage.getItem('xAuthToken');
const basicHeader = 'Basic ' + localStorage.getItem('credentials');
const headers = new Headers({
'x-auth-token': xToken,
'Authorization': basicHeader
});
return this.http.get(url, { headers: headers }); // Error at this line
}
logout() {
const url = 'http://localhost:8181/user/logout';
const xToken = localStorage.getItem('xAuthToken');
const basicHeader = 'Basic ' + localStorage.getItem('credentials');
const headers = new Headers({
'x-auth-token': xToken,
'Authorization': basicHeader
});
return this.http.get(url, { headers: headers }); // Error at this line
}
}
然后我收到错误消息:
(property) headers?: HttpHeaders | {
[header: string]: string | string[];
}
Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'.
Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'.
Index signature is missing in type 'Headers'.ts(2322)
http.d.ts(1086, 9): The expected type comes from property 'headers' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
在线return this.http.get(url, { headers: headers });
有人知道怎么解决吗?
【问题讨论】:
-
您可以发布您的
component代码吗?你在哪里打电话LoginService。
标签: java angular spring spring-boot spring-mvc