Javascript Angular 2 - Http Get 请求 - 传递 json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37994332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 2 - Http Get request - pass json Object
提问by hamras
How can I do a http get request and pass an json Object
如何执行 http get 请求并传递 json 对象
This is my json-Object
这是我的 json 对象
{{firstname:"Peter", lastname:"Test"}
and this Object I want to pass in the http request to get a list Of matched persons.
这个对象我想通过 http 请求来获取匹配人员的列表。
how is it possible? This example just shows a simple get request with a json result. How do I have to modify it?
这怎么可能?此示例仅显示了一个带有 json 结果的简单 get 请求。我该如何修改它?
//Component:
person:Person;
persons:Person [];
....
//Whre can I pass the person, here in the service??
getMatchedPersons(){
this.httpService.getMatchedPersons().subscribe(
data => this.persons = data,
error => aller(error)
);
);
//SERVICE
//pass parameters?? how to send the person object here?
getMatchedPersons(){
return this.http.get('url').map(res => res.json());
}
回答by Roman Skydan
The Http.get
method takes an object that implements RequestOptionsArgsas a second parameter.
该Http.get
方法将实现RequestOptionsArgs的对象作为第二个参数。
The search field of that object can be used to set a string or a URLSearchParamsobject.
该对象的搜索字段可用于设置字符串或URLSearchParams对象。
An example:
一个例子:
// Parameters obj-
let params: URLSearchParams = new URLSearchParams();
params.set('firstname', yourFirstNameData);
params.set('lastname', yourLastNameData);
//Http request-
return this.http.get('url', {
search: params
}).subscribe(
(response) => //some manipulation with response
);
回答by SparK
For pure javascript:
对于纯 javascript:
You must serialize your json to a list of parameters:
您必须将 json 序列化为参数列表:
?firstname=peter&lastname=test
and append it to the URL because GET
requests have no body.
并将其附加到 URL,因为GET
请求没有正文。
There are a few ways of converting JSON to QueryParameters. They are addressed in this question: Is there any native function to convert json to url parameters?
有几种方法可以将 JSON 转换为 QueryParameters。他们在这个问题中得到解决:是否有任何本机函数可以将 json 转换为 url 参数?
There you can choose the poison of your liking, mine was:
在那里你可以选择你喜欢的毒药,我的是:
function obj_to_query(obj) {
var parts = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
}
}
return "?" + parts.join('&');
}
But mind you that GET
requests must obbey the URL limit that based on this answeris recomended to stay under 2000characters to be safe:
但请注意,GET
请求必须遵守基于此答案的 URL 限制,建议保持在2000 个字符以下以确保安全:
RFC says 8000
IE8 and IE9 go as far as 2083
Search engines only read to 2048
RFC 说 8000
IE8 和 IE9 可以达到 2083
搜索引擎只能读取到 2048
Using Angular2 URLSearchParams
使用 Angular2 URLSearchParams
With the same method of converting a plain JSON to arguments one could use URLSearchParams
as suggested by Рома Скидан:
使用与URLSearchParams
Рома Скидан 建议的将普通 JSON 转换为参数的相同方法:
let params: URLSearchParams = objToSearchParams(obj);
return this.http.get('url', {
search: params
}).subscribe(
(response) => //some manipulation with response
);
function objToSearchParams(obj): URLSearchParams{
let params: URLSearchParams = new URLSearchParams();
for (var key in obj) {
if (obj.hasOwnProperty(key))
params.set(key, obj[key]);
}
return params;
}
回答by AngJobs on Github
Maybe you want to stringify the json object
也许您想对 json 对象进行字符串化
var json = JSON.stringify(myObj);
this.http.get('url'+'?myobj='+encodeURIComponent(json))
回答by rinukkusu
Use a POST
request to pass objects to the server:
使用POST
请求将对象传递给服务器:
//SERVICE
getMatchedPersons(searchObj: any){
return this.http.post('url', JSON.stringify(searchObj))
.map(res => res.json());
}
Then you can pass whatever JS object you want and send it to the server in your http request.
然后你可以传递你想要的任何 JS 对象,并在你的 http 请求中将它发送到服务器。
getMatchedPersons(searchObj: any){
this.httpService.getMatchedPersons(searchObj: any).subscribe(
data => this.persons = data,
error => alert(error);
);
);