import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http'; import { Router } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import { UserProfile } from '../model/data-model'; //import { AppSettings, User } from '../model/data-model'; @Injectable() export class UpdateUserService { //appSettingsLoaded: boolean; //appSettings: AppSettings; //currentUser: User; //loggedIn: boolean; userId: number; firstName: string; lastName: string; emailId: string; userInfo: UserProfile; private headers: Headers; constructor(private http: Http, private router: Router) { //if (this.currentUser == null) { // var user = localStorage.getItem("currentUser"); // if (user != null) { // this.currentUser = JSON.parse(user); // this.loggedIn = true; // } //} this.userId = 0; this.firstName = ""; this.lastName = ""; this.emailId = ""; } private extractData(res: Response) { let body = res.json(); return body || {}; } private handleError(error: Response | any) { // In a real world app, we might use a remote logging infrastructure let errMsg: string; if (error instanceof Response) { const body = error.json() || ''; const err = body.error || JSON.stringify(body); errMsg = `${error.status} - ${error.statusText || ''} ${err}`; } else { errMsg = error.message ? error.message : error.toString(); } console.error(errMsg); return Observable.throw(errMsg); } public UpdateUserProfile(applicationName: string, strFirstName: string, strLastName: string, strEmailID: string): Observable { console.log('inside application service'); let usrID: number; usrID = 1; let headers = new Headers({ 'Content-Type': 'application/json' }); //let headers = new Headers(); //headers.append('Content-type', 'application/x-www-form-urlencoded'); let options = new RequestOptions({ headers: headers }); let body = JSON.stringify({ userId: usrID, emailId: strEmailID, firstName: strFirstName, lastName: strLastName }); console.log(body); return this.http.post('http://localhost:85/AIAHTML5.Server/api/updateprofile', body, options) .map(this.extractData) .catch(this.handleError); } public UpdateUserProfile2(applicationName: string, strFirstName: string, strLastName: string, strEmailID: string): Observable { console.log('inside update-user service -2'); let usrID: number; usrID = 1; //let headers = new Headers({ 'Content-Type': 'application/json' }); let headers = new Headers(); headers.append('Content-type', 'application/x-www-form-urlencoded'); let options = new RequestOptions({ headers: headers }); let body = 'userId=' + usrID + '&emailId=' + strEmailID + '&firstName=' + strFirstName + '&lastName=' + strLastName; console.log(body); let urlSearchParams = new URLSearchParams(); urlSearchParams.append('userId', usrID.toString()); urlSearchParams.append('firstName', strFirstName); urlSearchParams.append('lastName', strLastName); urlSearchParams.append('emailId', strEmailID); let body2 = urlSearchParams.toString() return this.http.post('http://localhost:85/AIAHTML5.Server/api/updateprofile', body, options) .map((response: Response) => { console.log(response); var body = response.json(); console.log(body); }) .catch(this.handleError); } }