0

I am doing ionic 2 login using phone number and password. I am using Laravel 5.3 passport as backend. As login is functioned using postman. but in ionic 2 it gives me error as -

Unexpected token < in JSON at position 0 at JSON.parse () at Response.Body.json (http.es5.js:800) at SafeSubscriber._next (register-api.ts:33)
my code is given below-

loginData = { contact:'', password:'' };

login.ts code 

doLogin() {
this.showLoader();   
this.registerApi.login(this.loginData).then((result) => {
  this.loading.dismiss();
  this.data = result; 
   this.viewCtrl.dismiss().then(
        (result) => this.app.getRootNav().setRoot(MyApp)
    )  
  window.location.reload(); 
  }, (err) => {
  this.loading.dismiss();
  this.presentToast(err);
});   }

registerapi.ts code

login(credentials) {
return new Promise((resolve, reject) => {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post(apiUrl+'login', JSON.stringify(credentials), {headers: headers})
      .subscribe(res => {
        resolve(res.json()); 

      }, (err) => {
        reject(err);   
      });
});  }

I am really stuck here.. Thank You.

MY php laravel code is as -

  public function login(){
    if(Auth::attempt(['contact' => request('contact'), 'password' => request('password')])){
        $user = Auth::user();
        $success['token'] =  $user->createToken('MyApp')->accessToken;
         $success['fname'] =  $user->fname;
      $success['lname'] =  $user->lname;
       $success['contact'] =  $user->contact;           
       $success['id'] =  $user->id;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}
Sanchit Mahajan
  • 315
  • 6
  • 16

1 Answers1

1

< in JSON at position 0 Means that most probably you are getting HTML body from the server, due to wrong link apiUrl (maybe instead of apiUrl +'login' should be apiUrl +'/login') or there is an php error, try following code to see the exact response in the console:

login(credentials) {
return new Promise((resolve, reject) => {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post(apiUrl+'login', JSON.stringify(credentials), {headers: headers})
      .subscribe(res => {
           console.log(res.text()); 

      }, (err) => {
        reject(err);   
      });
});  }
Roman Habibi
  • 604
  • 5
  • 8
  • above code show error as - Property 'then' does not exist on type 'String' – Sanchit Mahajan Oct 26 '17 at 17:04
  • All values are displayed on console... thank you sir,,.. but one warning as - Warning: Cannot modify header information - headers already sent in Unknown on line 0
    – Sanchit Mahajan Oct 26 '17 at 17:57
  • On the server side you need to make sure that php warnings logging is turned off and so that you return pure `JSON` formatted text without any html tags and use your original code with `.json()` – Roman Habibi Oct 26 '17 at 19:17
  • See following thread to deal with headers problem: https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Roman Habibi Oct 27 '17 at 22:34
  • @SanchitMahajan good to hear that the problem is solved! At least you know now how to debug and to trace the cause!) – Roman Habibi Oct 31 '17 at 08:12