使用Fetch API传递参数
从Angular HTTPClient转换代码后,我正在尝试使用fetch传递参数(这是任务的一部分)。在我对其进行更改之前,这是代码的一部分。
let activeUrl = new URL(this.serverAddress);
let targetUrl = activeUrl.origin + environment.apiBasePath + '/named_selection/' + componentId;
let params = new HttpParams().set('name', name);
if (this.customLookupAddress != "") {
params.set('lookup', this.customLookupAddress);
}
if (this.customGatewayAddress != "") {
params.set('gateway', this.customGatewayAddress);
}
return this.httpClient.get(targetUrl, { headers: { 'responseType': 'json' }, params: params }).pipe(
map((namedSelection) => {
this.namedSelections.set(componentId.toString() + '.' + name, namedSelection);
我将整个代码转换为fetch api的用法。此处的外观:
let activeUrl = new URL(this.serverAddress);
let targetUrl = activeUrl.origin + environment.apiBasePath + '/named_selection/' + componentId;
let params = new HttpParams().set('name', name);
if (this.customLookupAddress != "") {
params.set('lookup', this.customLookupAddress);
}
if (this.customGatewayAddress != "") {
params.set('gateway', this.customGatewayAddress);
}
const data$ = new Observable(observer => {
fetch(targetUrl, { headers: { 'responseType': 'json'}, method: 'GET'})
.then(response => response.json())
.then(namedSelection => {
observer.next(namedSelection);
observer.complete();
})
.catch(err => observer.error(err));
});
return data$.pipe(
tap((namedSelection) => {
this.namedSelections.set(componentId.toString() + '.' + name, namedSelection);
})
);
}
但是,在这种情况下,我无法传递参数“参数”。你们能否帮助我了解如何进行操作?在获取功能的一部分方面应该如何构建代码?
I am trying to pass parameters using fetch after converting the code from angular httpclient (it was part of the task). Herein is the part of the code before I made changes to it.
let activeUrl = new URL(this.serverAddress);
let targetUrl = activeUrl.origin + environment.apiBasePath + '/named_selection/' + componentId;
let params = new HttpParams().set('name', name);
if (this.customLookupAddress != "") {
params.set('lookup', this.customLookupAddress);
}
if (this.customGatewayAddress != "") {
params.set('gateway', this.customGatewayAddress);
}
return this.httpClient.get(targetUrl, { headers: { 'responseType': 'json' }, params: params }).pipe(
map((namedSelection) => {
this.namedSelections.set(componentId.toString() + '.' + name, namedSelection);
I converted the entire code the usage of fetch API. Herein what it looks like now:
let activeUrl = new URL(this.serverAddress);
let targetUrl = activeUrl.origin + environment.apiBasePath + '/named_selection/' + componentId;
let params = new HttpParams().set('name', name);
if (this.customLookupAddress != "") {
params.set('lookup', this.customLookupAddress);
}
if (this.customGatewayAddress != "") {
params.set('gateway', this.customGatewayAddress);
}
const data$ = new Observable(observer => {
fetch(targetUrl, { headers: { 'responseType': 'json'}, method: 'GET'})
.then(response => response.json())
.then(namedSelection => {
observer.next(namedSelection);
observer.complete();
})
.catch(err => observer.error(err));
});
return data$.pipe(
tap((namedSelection) => {
this.namedSelections.set(componentId.toString() + '.' + name, namedSelection);
})
);
}
However, I am unable to pass in the parameters 'params' in this case. Can you guys please help me out on how to go about it and how should it be structure the code when it comes to the part of the fetch function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用
fetch
将URL参数添加到请求中,您需要将它们附加到fetch url(还设置了正确的标题名称):To add URL params to a request using
fetch
, you need to append them to the fetch url (and also set the correct header names):