3 回答

TA貢獻1799條經驗 獲得超8個贊
headers
嘗試這樣設置:
let headers = new HttpHeaders(); headers = headers.set('x-auth-token', xToken).set('Authorization', basicHeader);
進而,
return this.http.post(url, null, headers );
null
按照它body
在第二個參數中接受的方式傳遞。
使用 HttpClient:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
在 app.module.ts中:
import { HttpClientModule } from '@angular/common/http';
在@NgModule 中: imports: [ HttpClientModule ]

TA貢獻1777條經驗 獲得超10個贊
這很正常,默認情況下@RequestMapping("/user/logout")只接受GET請求。您必須明確設置方法
@RequestMapping("/user/logout", method = RequestMethod.POST)
public ResponseEntity<String> logout(){
SecurityContextHolder.clearContext();
return new ResponseEntity<String>("Logout Successfully!", HttpStatus.OK);
}
或者使用@PostMapping

TA貢獻1865條經驗 獲得超7個贊
嘗試這個:
constructor(private http:HttpClient){}
logout()
{
const url = 'http://localhost:8181/user/logout';
const headers = new HttpHeaders()
.set('x-auth-token', localStorage.getItem('xAuthToken'));
return this.http.post(url, '' ,{headers: headers, responseType: 'text'})};
在 Spring 中,我的建議是使用@PostMappingor @DeleteMappingannotation 而不是@RequestMapping. 使用 as 'text' 的原因ResponseType是因為您提供的是ResponseEntity<>as ofString類型,并且默認情況下 Angular 將響應視為 JSON。
localStorage.removeItem('xAuthToken');此外,請記住在訂閱該可觀察對象時在響應中使用,并在ngOnDestroy()生命周期中取消訂閱該可觀察對象。
添加回答
舉報