當我呼叫 API 以根據 API 中的傳遞 id 獲取特定的資料串列時,它可以正常作業并將物件作為資料串列回傳。但是如果 API 在資料庫中不存在提供的 id,那么它會回傳字串,就像在 spring boot 中一樣回傳“對不起,我們的串列中不存在這個 id”;
這樣我們就可以通過 chrome 的檢查找到字串回應。但是我們如何以角度獲取該字串,以便我們可以顯示來自 API 的用戶回應訊息。
uj5u.com熱心網友回復:
您可以嘗試以下代碼片段。希望這對你有用。
returnObservableAsStringResp(): Observable<any> {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
const requestOptions: Object = {
headers: headers,
responseType: 'text'
}
return this.http.get<any>(this.apiURL , requestOptions);
}
uj5u.com熱心網友回復:
public call(id: any) {
this.Student = [];
this.httpClient.getfriends(id).subscribe((respo) => {
for (const d of (respo as any)) {
this.Student.push({
id: d.id,
name: d.name,
profilepic: d.profilepic,
address: d.address,
mobile: d.mobile,
isfriend: d.isfriend
});
}
console.warn("resp", respo)
},
(data: HttpErrorResponse) => {
alert(data.error.text);
console.warn(data.error.text);
}
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339010.html
