我将如何在 angular 应用程序的服务内将查询参数传递给 REST API?
How will i pass query parameters to REST API inside a service in an angular application?
我正在尝试将服务内的查询参数传递给 REST API。
我应该如何传递给 API 的示例。(预期)
http://localhost:2000/world/123456789/avengers?type=fruits&fields=_all
试过如下:
all(countId) {
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
let params = new HttpParams();
params = params.append("type", "fruits");
params = params.append("fields", "_all");
const options = {
headers: headers,
params: params
};
return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )
}
但我无法通过他们的查询 parameters.how 我会这样做吗?
您可以将它们作为参数发送
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
let params = new HttpParams().set('type',fruits).set('fields',_all);
return this.http.put ( "http://localhost:2000/world/123456789/avengers",{ params: params, headers: headers})
或者您可以发送选项
let options: HttpOptions;
options.headers = headers;
options.params = params;
return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )
因为 HTTP PUT/POST
发送正文时没有在 URL 中附加查询字符串(你可以使用一些库来构建查询字符串)所以你需要构建你的 URL 和选项
* @param url The endpoint URL.
* @param body The resources to add/update.
* @param options HTTP options
*
* @return An `Observable` of the response, with the response body as a JSON object.
*/
put(url: string, body: any | null, options?: {}
all(countId) {
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
const options = {
headers: headers,
params: params
};
return this.http.put ( "http://localhost:2000/world/123456789/avengers??type=fruits&fields=_all", options)
}
您目前正在将您的选项作为请求负载发送,就像您发现的那样。如果没有payload,可以通过null
:
all(countId) {
// ....
return this.http.put("...", null, options)
^^^^
}
/********** 这是我的组件文件 ***********/
page: any;
error: {};
orderObj: {};
ngOnInit(){
const type: string = this.route.snapshot.queryParamMap.get('type');
const fields: string = this.route.snapshot.queryParamMap.get('fields');
this.orderObj = {'type' : type, 'fields' : fields}
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
this.cmspageService.getPage(params.get('slug'), this.orderObj)
)
).subscribe(
(data: any) => this.page = data,
error => this.error = error
);
}
/********** 这是我的服务文件 ***********/
public queryStringUrl : string
getPage(slug: string, data:object){
if(data){
this.queryStringUrl = '?';
let i = 0;
for (var p in data) {
if (data.hasOwnProperty(p)){
if(i != 0)
this.queryStringUrl += '&';
this.queryStringUrl += p + '=' + data[p];
}
i++;
}
//return this.queryStringUrl;
// alert(this.queryStringUrl);
}
return this.http.get<Page>('http://localhost/dev/blogger/api/page/' + slug + this.queryStringUrl,)
.pipe(
catchError(this.handleError)
);
}
我正在尝试将服务内的查询参数传递给 REST API。
我应该如何传递给 API 的示例。(预期)
http://localhost:2000/world/123456789/avengers?type=fruits&fields=_all
试过如下:
all(countId) {
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
let params = new HttpParams();
params = params.append("type", "fruits");
params = params.append("fields", "_all");
const options = {
headers: headers,
params: params
};
return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )
}
但我无法通过他们的查询 parameters.how 我会这样做吗?
您可以将它们作为参数发送
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
let params = new HttpParams().set('type',fruits).set('fields',_all);
return this.http.put ( "http://localhost:2000/world/123456789/avengers",{ params: params, headers: headers})
或者您可以发送选项
let options: HttpOptions;
options.headers = headers;
options.params = params;
return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )
因为 HTTP PUT/POST
发送正文时没有在 URL 中附加查询字符串(你可以使用一些库来构建查询字符串)所以你需要构建你的 URL 和选项
* @param url The endpoint URL.
* @param body The resources to add/update.
* @param options HTTP options
*
* @return An `Observable` of the response, with the response body as a JSON object.
*/
put(url: string, body: any | null, options?: {}
all(countId) {
const headers: HttpHeaders = new HttpHeaders({
"_id" : countId,
"content-type" : "application/json"
});
const options = {
headers: headers,
params: params
};
return this.http.put ( "http://localhost:2000/world/123456789/avengers??type=fruits&fields=_all", options)
}
您目前正在将您的选项作为请求负载发送,就像您发现的那样。如果没有payload,可以通过null
:
all(countId) {
// ....
return this.http.put("...", null, options)
^^^^
}
/********** 这是我的组件文件 ***********/
page: any;
error: {};
orderObj: {};
ngOnInit(){
const type: string = this.route.snapshot.queryParamMap.get('type');
const fields: string = this.route.snapshot.queryParamMap.get('fields');
this.orderObj = {'type' : type, 'fields' : fields}
this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
this.cmspageService.getPage(params.get('slug'), this.orderObj)
)
).subscribe(
(data: any) => this.page = data,
error => this.error = error
);
}
/********** 这是我的服务文件 ***********/
public queryStringUrl : string
getPage(slug: string, data:object){
if(data){
this.queryStringUrl = '?';
let i = 0;
for (var p in data) {
if (data.hasOwnProperty(p)){
if(i != 0)
this.queryStringUrl += '&';
this.queryStringUrl += p + '=' + data[p];
}
i++;
}
//return this.queryStringUrl;
// alert(this.queryStringUrl);
}
return this.http.get<Page>('http://localhost/dev/blogger/api/page/' + slug + this.queryStringUrl,)
.pipe(
catchError(this.handleError)
);
}