1

Hi everyone am new to angular 2+ and ionic 3 and am trying to post a login form from my ionic 3 app to my asp.net web api application to perform a token based authentication but i get this error : converting circular structure to json error

This is the component code :

  @Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers:[LoginService],
  })
  export class LoginPageComponent {

  errorMessage:string;
  loginModel = new ILoginModel();
  constructor(public navCtrl: NavController,private loginService: 
  LoginService) {}
  token: string;

  logUser(){

     this.loginService.login(this.loginModel)
    .subscribe(token => this.token= token,
    error => this.errorMessage = <any>error);

  }
  }

This is the service code :

@Injectable()
export class LoginService{

loginModel: ILoginModel;
urlBase:string = 'http://localhost:1487/';
header = new HttpHeaders()
              .set('content-type','application/x-www-form-urlencoded');
token :any;


constructor(private _http: HttpClient){}


  login(loginModel:ILoginModel):Observable<string>{
     this._http.post(this.urlBase 
     +'token','username='+this.loginModel.userName+
   '&password='+this.loginModel.password+'&grant_type=
  '+this.loginModel.grant_type)
     .map((response: HttpResponse<string>)=> <string>response.body)
    //  .do(data => console.log('Response Token: '+JSON.stringify(data)))
     .catch(this.handleError)
     .subscribe((res: HttpResponse<string>)=>{this.token = res.body});
     return this.token;


    }

    private handleError(error: HttpResponse<any>) {
    console.error(error);
    return Observable.throw(error.body().JSON() || 'Server Error');
  }

  }

Thank you.

1 Answer 1

1

You're using the subscribe method twice. Change this

login(loginModel:ILoginModel){
     return this._http.post(this.urlBase 
     +'token','username='+loginModel.userName+
   '&password='+loginModel.password+'&grant_type=
  '+loginModel.grant_type)
     .map(response=> <string>response.body.json())
    //  .do(data => console.log('Response Token: '+JSON.stringify(data)))
     .catch(this.handleError)
    }

change this too.

 token: any;

  logUser(){

     this.loginService.login(this.loginModel)
    .subscribe(token => {this.token= token,
                        console.log(this.token)},
    error => this.errorMessage = <any>error);

  }
  }
Sign up to request clarification or add additional context in comments.

4 Comments

Can you check again ?
It raises an error in the code : "Cannot asign type Observable<ArrayBuffer> To Observable<string>"
Thank you Melchia , it is working now and i marked your answer as valid , But the challenge for me now is to make it work with Observables.
You are returning an Observable & you're subscribing on it in your logUser method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.