所以我試圖用http客戶端對我的rest api進行POST。我只需要發送一個 ID,但它根本沒有做任何事情,就好像它不存在一樣。沒有錯誤資訊等。
試過這樣:
httpOptions = {
headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type") " " localStorage.getItem("token")})
};
public likePet(id : string){
var url = environment.apiKey "/match/cr";
const params = new HttpParams()
.set('id', id)
console.log(url);
try{
console.log("t1");
this.httpClient.post(url,{params: params},this.httpOptions);
} catch (error){
console.error(error);
}
console.log("t2");
}
這樣:
httpOptions = {
headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type") " " localStorage.getItem("token")})
};
public createMatch(matchid: number){
console.log("////");
return this.httpClient.post(environment.apiKey "match/cr?id=" matchid,this.httpOptions).pipe(
catchError((err) => {
console.error(err);
window.alert("Failed");
throw err
})
)
}
嘗試使用完整的硬編碼網址,例如:localhost:8080/api/match/cr?id=1
甚至沒有錯誤訊息。API 作業正常。我的其他 http GET/POST 方法有效。但這是我第一次沒有發送 JSON 的 POST 并且我一無所知。
uj5u.com熱心網友回復:
該post()方法回傳一個 Observable。就我而言,一旦您訂閱了該 Observable,就會立即發送呼叫。無論你在哪里打電話createMatch()試試看createMatch(matchId).subscribe()有沒有什么事情發生。
如果您不想使用 Observables,但希望將您的回應作為 Promise,您可以撰寫
httpOptions = {
headers: new HttpHeaders({ 'Authorization': localStorage.getItem("type") " " localStorage.getItem("token")})
};
public createMatch(matchid: number){
console.log("////");
return this.httpClient.post(environment.apiKey "match/cr?id=" matchid,this.httpOptions).pipe(
catchError((err) => {
console.error(err);
window.alert("Failed");
throw err
})
).toPromise()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463598.html
上一篇:HCNP Routing&Switching之代理ARP
下一篇:如何逐行制作燒瓶流輸出?
