Reputation: 21630
THE SITUATION:
Sorry if it is a basic question.
In my Ionic 2 app I have a POST request for the login. It is properly working. There is a form with email and password. I send this data to the API and I get as response the corresponding user data.
The only problem is that I get this data inside the service.
How can then pass this data to the loginPage component?
In the console i can see all the data properly received. Just the console.log is inside the service.
I only need to pass the data to the component.
THE CODE:
The LoginPage component:
makeLogin()
{
var email = this.loginForm.value.email.trim();
var password = this.loginForm.value.password.trim();
this.userService.submitLogin(email, password, this.api_access_key);
}
The service:
submitLogin(email: string, password: string, api_access_key: string)
{
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('email', email);
urlSearchParams.append('password', password);
urlSearchParams.append('api_access_key', api_access_key);
let body = urlSearchParams.toString()
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
this.http
.post(this.loginUrl, body, { headers: headers })
.map(response => response.json())
.subscribe(data => {
console.log('login API success');
console.log(data);
}, error => {
console.log(error);
});
}
Upvotes: 0
Views: 778
Reputation: 13558
You need to subscribe
inside your component
not in service
.
// component
makeLogin()
{
var email = this.loginForm.value.email.trim();
var password = this.loginForm.value.password.trim();
this.userService.submitLogin(email, password, this.api_access_key).subscribe((res) => {
console.log(res);
}, (error) => {
console.log(error);
});
}
// Service
submitLogin(email: string, password: string, api_access_key: string)
{
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('email', email);
urlSearchParams.append('password', password);
urlSearchParams.append('api_access_key', api_access_key);
let body = urlSearchParams.toString()
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http
.post(this.loginUrl, body, { headers: headers })
.map(response => response.json());
}
Upvotes: 1