请确保您有正确的进口,
import {Http, Response, URLSearchParams} from '@angular/http';
这是如何使一个GET请求,
Get请求
saveProfile(model: Profile, isValid: boolean) {
let params: URLSearchParams = new URLSearchParams();
// set params to go to URL
params.set('email', model.email);
params.set('first_name', model.first_name);
return this.http.get('url/path/here/dont/forget/port',
{ search: params })
.map((res: Response) => res.json())
.subscribe((res) => {
console.log(res);
// Map the values in the response to useable variables
this.auth.user.email = res.user.email;
this.auth.user.first_name = res.user.first_name;
});
}
}
帖子要求
如何发布帖子请求,这是auth0库中常用的帖子请求。你可以发现here
authenticate(username, password) {
let creds = JSON.stringify({ username: username.value, password: password.value });
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('http://localhost:3001/sessions/create', creds, {
headers: headers
})
.subscribe(
data => {
this.saveJwt(data.json().id_token);
username.value = null;
password.value = null;
},
err => this.logError(err.json().message),
() => console.log('Authentication Complete')
);
}
这些例子将得到服务器的响应。如果你想做更多技术性的事情,比如在视图中获取新的数据来更新,你将不得不创建一个observable
。如果我是你,我会得到这个,然后当你需要了解observable
你可以合并。