0

I have one issue in $http request errorCallback function using angular.js and php.Here i have one login page when i am entering wrong username/password the values always return to successCallback function.I am explaining my code below.

loginController.js:

var loginAdmin=angular.module('Channabasavashwara');
loginAdmin.controller('loginController',function($scope,$http,$location){
    $scope.user_name = ''; 
    $scope.user_pass = '';
    $scope.user_type= '';
    $scope.user_login=function(){
    if($scope.user_name==''){
        alert('user name filed should not keep blank');
    }else if($scope.user_pass==''){
        alert('password filed should not keep blank');
    }else if($scope.user_type==''){
        alert('Please select your user type');
    }else{
        var userData={'user_name':$scope.user_name,'user_pass':$scope.user_pass};
        if($scope.user_type=='admin'){
        $http({
            method: 'POST',
            url: "php/Login/login.php",
            data: userData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            console.log('response',response);
            alert(response.data['msg']);
            $location.path('dashboard');
        },function errorCallback(response) {
            alert(response.data['msg']);
        });
    }
    if($scope.user_type=='hod'){
        $http({
            method: 'POST',
            url: "php/Login/hodLogin.php",
            data:userdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            alert(response.data['msg']);
            $location.path('dashboard');
        },function errorCallback(response) {
            alert(response.data['msg']);
        });
    }
    }
    }
});

login.php:

<?php header("HTTP/1.0 401 Unauthorized");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user_name=$request->user_name;
$user_pass=$request->user_pass;
$connect = mysql_connect("localhost", "root", "****");
mysql_select_db('go_fasto', $connect);
$selquery = "SELECT * FROM db_Admin_Master WHERE user_name='".$user_name."' and password='".$user_pass."'";
$selres = mysql_query($selquery);
if(mysql_num_rows($selres ) > 0){
    $result=mysql_fetch_array($selres);
    $result['msg'] = 'Login successfull...';
}else{
    $result['msg'] = 'You entered wrong username/password';
}
echo json_encode($result);
?>

Here my problem is suppose user entered wrong user name and password the message(i.e-You entered wrong username/password) is always returning to successCallback function which should return to errorCallback function.Here my requirement is the success message will return to seccessCallback function and the error message will return to errorCallback function.Please help me.

7
  • Please try setting a HTTP header with status code to an error may be a 401 <?php header("HTTP/1.0 401 Unauthorized"); ?> Commented Oct 8, 2015 at 5:43
  • By just echoing the message your callback wouldn't know if an error has occurred or not. So you need to set a header. Commented Oct 8, 2015 at 5:45
  • @maniteja : Can you please edit your answer ? Commented Oct 8, 2015 at 5:47
  • @maniteja : I did as per you for both case the errorCallBack function is executing. Commented Oct 8, 2015 at 5:50
  • please share the updated code. Commented Oct 8, 2015 at 5:52

1 Answer 1

1

Try Setting a error HTTP header like below that will let your callback know that there has been an error on the PHP side.

<?php 
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$user_name=$request->user_name;
$user_pass=$request->user_pass;
$connect = mysql_connect("localhost", "root", "Oditek123@");
mysql_select_db('go_fasto', $connect);
$selquery = "SELECT * FROM db_Admin_Master WHERE user_name='".$user_name."' and password='".$user_pass."'";
$selres = mysql_query($selquery);
if(mysql_num_rows($selres ) > 0){
$result=mysql_fetch_array($selres);
$result['msg'] = 'Login successfull...';
}else{
header("HTTP/1.0 401 Unauthorized");
$result['msg'] = 'You entered wrong username/password';
}
echo json_encode($result);
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.