我将如何将查询参数传递给 Angular 应用程序中服务内的 REST API? [英] How will i pass query parameters to REST API inside a service in an angular application?
本文介绍了我将如何将查询参数传递给 Angular 应用程序中服务内的 REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将服务中的查询参数传递给 REST API.
I'm trying to pass query parameters inside a service to a REST API.
我应该如何传递给 API 的示例.(预期)
Example of how i should pass to API.(EXPECTED)
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 )
}
但我无法将查询参数传递给它们.我该怎么做?
But i am not able to pass them has query parameters.how will i do that?
推荐答案
您当前正在将您的选项作为请求有效负载发送,就像您发现的那样.如果您没有有效负载,则可以传递 null
:
You are currently sending your options as the request payload, like you have discovered. If you do not have a payload, you can pass null
:
all(countId) {
// ....
return this.http.put("...", null, options)
^^^^
}
这篇关于我将如何将查询参数传递给 Angular 应用程序中服务内的 REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文