Skip to content

Instantly share code, notes, and snippets.

@lricoy
Last active April 14, 2016 01:24
Show Gist options
  • Save lricoy/8fd0edef0e4ed2f715946df543826b88 to your computer and use it in GitHub Desktop.
Save lricoy/8fd0edef0e4ed2f715946df543826b88 to your computer and use it in GitHub Desktop.
login(obj) {
this.postRequest('http://localhost:8888/api/student_auth/login', obj).subscribe(
function(result) { console.log('result', result); }
);
// Não vai funcionar porque o callback acima do subscribe é assíncrono
// return result;
}
postRequest(url, data) {
this.headers = new Headers();
this.headers.append('Content-Type', 'application/x-www-form-urlencoded');
//this.headers.append("Content-Type", 'application/json');    
//this.headers.append("Authorization", 'Bearer ' + localStorage.getItem('id_token'));
  this.requestoptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: this.headers,
body: JSON.stringify(data)
});
let getObs = this.http.request(new Request(this.requestoptions))
.map((res) => {
return res.json()
});
getObs.subscribe(
data => {
if(data.success) {
this.setTokenLocalStorage(data.token);
}
},
err => {
return false;
},
() => console.log('done')
);
return getObs;
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment