我试图在角度2中获取具有键值对的json对象。
Your question is not very clear. But I will demonstrate some things as I understood from this question.
Here's a sample code (Assuming the value pair as username & password )
let params:URLSearchParams = new URLSearchParams();
params.set('username', username);
params.set("password", password);
return this.http.post(this.baseUrl + '/auth/credential', '', {search: params}).map((res:Response) => res.json());
I defined above method getUserDetails() as a Global service and I import it as the dataservice to my particular component which mentioned below,Assuming the server send the reply inside a array called results (cant say much about that with out looking at the back-end implementation of your project)
this.dataService.getUserDetails().subscribe(
(data) => {
console.log('fetched userdata for edit', data.results)
this.modify_users = data.results;
console.log(data.results);
console.log(this.modify_users);
},
(error) => {
console.log('Failure viewUserDetails');
alert('Error getting user Details to edit');
});
So if I simply describe on what I'm doing here is,
(data) =>{} : to define what to do next when you receive any kind of Data from the server.
(error)=>{} : to define what to do next when the server replied withan error
you have to use http request to make get request like this :-
import {Injectable} from '@angular/core';
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';
@Injectable()
export class GlobalService {
constructor(public http: Http) { }
MethodName(): any {
return this.http.get('your_json_Path')
.map(res=> res.json())
.subscribe(res => {
// your stuff here
});
}
}
See also
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.