請確保您有正確的進口,
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
你可以合併。