Thursday, March 16, 2017

How to return json reponse from laravel to angular?

I have this in controller :

public function index(Request $request){
  $email = $request->email;
 $password = $request->password;
if (!$email || !$password) {return redirect()->back();}
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    // Authentication passed...
    $this->loggedUser = Auth::user();
    if($this->loggedUser){

        return response()->json(['isLoggedIn' => true],200);
    }

}
return response()->json(['isLoggedIn' => false],200);

}

In angular i have this:

Login (body: Object): Observable<Login[]> {
        let bodyString = JSON.stringify(body); // Stringify payload

        let options = new RequestOptions({ headers: this.headers }); // Create a request option

        return this.http.post('/index', body, options) // ...using post request
                         .map(response => {return response} ,console.log('aaa')) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error' )); //...errors if any
    }

Problem is that when i open in browser response i get this:


Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0
{"isLoggedIn":false

}

Any suggestion how can fix that so that in response i get json?



via uzhas

Advertisement