我有一個回傳玩家圖片的獲取請求,但如果沒有實際玩家的圖片,我會收到錯誤代碼 403,并且什么也沒有顯示。
如何解決這個問題?
這是服務:
public GetPlayerImage(id: number): Promise<Blob>{
return this.http.get<Blob>('https://divanscore.p.rapidapi.com/players/get-image?playerId=' id, {
headers: {
'x-rapidapi-host': 'divanscore.p.rapidapi.com',
'x-rapidapi-key': 'XXXX'
},
responseType: 'blob' as 'json'
}).toPromise();
}
這是component.ts:
ngOnInit(): void {
this.route.queryParams.subscribe(async (params) => {
if (params.playerId !== null && params.playerId !== undefined) {
this.player = await this.playerService.GetOnePlayer(params.playerId);
this.playerClub = await this.playerService.GetOnePlayerClub(params.clubId);
await this.getImageFromService();
this.isLoading = false;
}
});
}
createImageFromBlob(image: Blob) {
let reader = new FileReader();
reader.addEventListener('load', () => {
this.imageToShow = reader.result;
}, false);
if (image) {
reader.readAsDataURL(image);
}
}
async getImageFromService() {
this.createImageFromBlob(await this.playerService.GetPlayerImage(this.player.playerId));
}
更新
它現在顯示,但它在控制臺中仍然是紅色并指示錯誤......如何解決這個問題?
圖片
uj5u.com熱心網友回復:
第一的。你有攔截器嗎?
如果沒有,我們可以捕捉到:
async getImageFromService() {
const imageContent = await this.playerService.GetPlayerImage(this.player.playerId).catch((error) => {
// error will come here.
})
this.createImageFromBlob(content);
}
或者你可以嘗試/抓住
async getImageFromService() {
try {
const imageContent = await this.playerService.GetPlayerImage(this.player.playerId);
this.createImageFromBlob(content);
} catch (error) {
// the error will be there
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/432083.html
