admin管理员组文章数量:1122846
Using angular 18 I am trying to send a GET
request to my api.
export class ClinicAnalyticsService {
private routeBase : string = `${environment.apiBase}/api/analytics`
constructor(private http: HttpClient) { }
getAll = (page: number) : Observable<ClinicService> =>
this.http.get(`${environment.apiBase}/api/analytics`, {
params: new HttpParams()
.set('page', page)
.set('limit', 10)
}).pipe(
map(({data}: any) => ({
...data,
count: data.count,
analytics : data.analytics.map((clinic: any) => ({...clinic, page}))
})
)
)
I have tried adding query parameters without using HttpParams
`${this.routeBase}?page=${page}&limit=10`
I have tried adding query parameter with the HttpParams
library as seen above. For some reason both outcomes produce this URL
http://127.0.0.1:5000/api/analytics/?page=1&limit=10
why is this occuring?
Using angular 18 I am trying to send a GET
request to my api.
export class ClinicAnalyticsService {
private routeBase : string = `${environment.apiBase}/api/analytics`
constructor(private http: HttpClient) { }
getAll = (page: number) : Observable<ClinicService> =>
this.http.get(`${environment.apiBase}/api/analytics`, {
params: new HttpParams()
.set('page', page)
.set('limit', 10)
}).pipe(
map(({data}: any) => ({
...data,
count: data.count,
analytics : data.analytics.map((clinic: any) => ({...clinic, page}))
})
)
)
I have tried adding query parameters without using HttpParams
`${this.routeBase}?page=${page}&limit=10`
I have tried adding query parameter with the HttpParams
library as seen above. For some reason both outcomes produce this URL
http://127.0.0.1:5000/api/analytics/?page=1&limit=10
why is this occuring?
Share Improve this question edited Nov 21, 2024 at 20:07 Mark Hill asked Nov 21, 2024 at 20:01 Mark HillMark Hill 1,8392 gold badges17 silver badges35 bronze badges 5 |1 Answer
Reset to default 0I'm not using angular, but had many similar issues with React/React-Router with a whole different set of behaviors when compiled and deployed to IIS. Maybe not the best solution, but I went with the route of putting the api base in a configuration file and reading it at runtime. Note: in dev mode, I had the VITE server proxy set to my dev server target. Here are the functions I used, in case you find them useful:
let webSettings: any | undefined = undefined;
const fetchWebSettings = async () =>
{
if ( !webSettings )
{
const route = 'config/web-config.json';
const response = await fetch(
route, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
//'mode': "no-cors"
}
} );
if ( !response.ok )
{
throw new Error( `Failed to load: ${ route }` );
}
const settings = await response.json();
webSettings = settings;
}
return webSettings;
};
export const getApiTarget = async () =>
{
let target = "/api/";
/// - in production mode, fetch the web api address from web-config.json
if ( import.meta.env.MODE !== 'development' )
{
const webSettings = await fetchWebSettings();
target = `${ webSettings.apiTarget }/`;
}
return target;
};
To reference the config:
const apiTarget = await getApiTarget();
const apiRoute = `${apiTarget}route/call`;
本文标签: angularHttpClient adding slash before query params in URLStack Overflow
版权声明:本文标题:angular - HttpClient adding slash before query params in URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307512a1933337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
environment.apiBase
. Might be ending with "/" – underflow Commented Nov 21, 2024 at 20:05