I have a simple code here that uses json_encode and echo out the response in PHP. Here is my code:
app.js
$scope.login = function() {
var data = {
username: $scope.login.username,
password: $scope.login.password
};
$http.post('endpoints/login.php', data)
.success(function(response) {
if(response.success == "true") {
alert('nice');
} else {
alert('not nice');
}
})
.error(function(response) {
console.log(response);
});
};
login.php
$data = json_decode(file_get_contents("php://input"));
$stmt = $db->prepare('SELECT * FROM accounts WHERE username=? AND password=?');
$stmt->execute(array($data->username, $data->password));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row > 0)
{
$response = [
"success" => true
];
echo json_encode($response);
}
else
{
$response = [
"success" => false
];
echo json_encode($response);
}
It works perfectly but this part doesn't work:
if(response.success == "true") {
alert('nice');
} else {
alert('not nice');
}
When I add console.log(response) I get Object {success: true} or Object {success: false}.
Am I missing something here? Thank you.
console.log(response)and your answer might become clear.responseto have adataproperty.success(), the data itself is passed in. FYI,success()is being deprecated, so you should use normal promise methods, such asthen()