I keep receiving exception : Unexpected token P in JSON at position 0
I am trying to pass parameters for login page : username and password
service.ts:
@Injectable()
export class SchoolAppUsers {
loginApiUrl = ‘http://localhost/SchoolApp/include/userLogin.php’;
constructor(public http: Http) {
this.http=http;
}
login(username: string, password: string): Observable {
let body = JSON.stringify({ username: username , password: password });
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this.http.post(this.loginApiUrl, body).map(res => <User>(res.json()))
}
}
login.ts:
export class LoginPage {
login: {username?: string , password?: string} = {};
submitted = false;
constructor(public navCtrl: NavController, private schoolAppUsers: SchoolAppUsers) {
this.schoolAppUsers=schoolAppUsers;
}
onLogin(form) {
this.submitted = true;
if (form.valid) {
this.schoolAppUsers.login(this.login.username, this.login.password).subscribe(res=> {console.log(res)});
}
}
}
can someone plz help out !!
Have you checked server side code?
I have come across this error before when some one leaves an echo statement in the php end.
OMG thank you sooooo much
it wasn’t any echo , i had this
$username = $_POST['username'];
$password = $_POST['password'];
if($username == '' || $password == '')
{
echo 'Please Fill all fields!';
}
I removed it and now its ok , thank you
mr.surajrao
this is my server side code
<?php
header("Content-Type: application/json");
$response = array();
$username = $_POST['username'];
$password = $_POST['password'];
if ($_SERVER['REQUEST_METHOD']=='POST')
{
require_once 'DbOperation.php';
$db = new DbOperation();
$result = $db->userLogin($username, $password);
if($result){
$user = $db->getUserData($username);
while($row = $user->fetch_assoc()){
$temp=array();
$temp['UserID'] = $row['UserID'];
$temp['name'] = $row['name'];
$temp['username'] = $row['username'];
$temp['password'] = $row['password'];
$temp['role'] = $row['role'];
array_push($response,$temp);
}
} else {
$temp=array();
$temp['message'] = "Invalid username or password!";
array_push($response,$temp);
}
echo json_encode($response);
}
i checked by resteasy , its working fine regarding responses… but in ionic 2 i only keep getting the error message : [‘message’] = “Invalid username or password!”;
i dont know why !!