Im currently building a login system using angular 2 framework (ionic). This login have 2 post data which are:
Email Password
I've built the API and ran using postman. Everything is fine. It's returning desired response. But the angular 2 POST doesnt seems to do what it should do. For example this is my request code:
var data = JSON.stringify({email: email, password: password});
this.http.post('https://dev.domain.com/api/login', data)
.subscribe(
res => {
console.log(res);
loading.dismiss();
},
err => {
console.log("Error occured");
loading.dismiss();
}
);
Laravel returns 200 response and desired data but Angular 2 is always identifying the response as error. This is my Laravel codes:
public function login(Request $request) {
$data = $request->json()->all();
$email = $data['email'];
$password = $data['password'];
if (Auth::attempt(['email' => $email, 'password' => $password])) {
$user = User::where('email', $email)->first();
return response()->json(['data' => $user])->header('Content-Type', 'application/json');
} else {
$returnData = array(
'status' => 'error',
'message' => 'An error occurred!'
);
return response()->json($returnData, 500)->header('Content-Type', 'application/json');
}
}
I tried installing CORS plugin. Still seems to not solve this issue.
Can anyone help me on this? Thanks