[英]How to change a key value in a response using angular http
我有一個場景,我收到以下json
作為響應。
[{
"ImagePath": "/folder/images/products/1.jpg",
"LanguageId": 2,
"Id": 2,
},
{
"ImagePath": "/folder/images/products/2.jpg",
"LanguageId": 3,
"Id": 3,
}
]
這里我的域名為www.getimage.com
所以現在我結合url = domainname + imagepath
。
e.g. www.getimage.com/folder/images/products/2.jpg
但這里的問題是,每當我點擊 url 時,它都會重定向到另一個 url。
e.g. www.getimage.com/protected/cart/img/products/2.jpg
如何處理此圖像 url 重定向?
離開標題,您想將響應中的鍵值映射到不同的值嗎?
res.map( e => {
e.ImagePath = e.ImagePath.replace('/folder/images/','/protected/cart/img/');
})
盡管您帖子的其余部分似乎表明您在重定向方面存在問題,這似乎是一個完全不同的問題。
您應該提供更多信息(如何調用 get,...),但是,如果我理解得很好,我認為您總是會收到這種類型的數組,所以也許您可以使用pipe
和一些 ImagePath 修復它mapping
轉換,像這樣:
import { map } from 'rxjs/operators';
return this.http
.get<SearchImageResponse[]>(this.url, { params})
.pipe<SearchImageResponse[]>(
map ( (_responseArray: SearchImageResponse[]) => (_responseArray.map( image => image.ImagePath = 'www.getimage.com' + image.ImagePath ) ) )
);
顯然,使用您的網址、參數和類型...
或者更容易,如果你在subscribe
中做“你的東西”:
this.http
.get<SearchImageResponse[]>(this.url, { params})
.pipe<SearchGifResponse[]>(
map ( (_responseArray: SearchImageResponse[]) => (_responseArray.map( image => image.ImagePath = 'www.getimage.com' + image.ImagePath ) ) )
)
.subscribe((response) => {
// response should still be a SearchImageResponse[]
response = response.map( (_responseArray: SearchImageResponse[]) => (_responseArray.map( image => image.ImagePath = 'www.getimage.com' + image.ImagePath ) ) );
}
或者這個:
let transformedArray:SearchImageResponse[] = [];
this.http
.get<SearchImageResponse[]>(this.url, { params})
.pipe<SearchGifResponse[]>(
map ( (_responseArray: SearchImageResponse[]) => (_responseArray.map( image => image.ImagePath = 'www.getimage.com' + image.ImagePath ) ) )
)
.subscribe((response) => {
// response should still be a SearchImageResponse[]
this._historialBusqueda.forEach( _img => {
_img.ImagePath = 'www.getimage.com' + _img.ImagePath;
transformedArray.push(_img);
});
}
抱歉,我現在無法測試,我不確定這是否有效。 但我希望它有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.