1

I am new to Angularjs and I am facing an issue while parsing a JSON response. I have created login page and for accessing data I have used JSON File. I am getting error when calling factory function.

HTML:

<form ng-submit="loginform(logcred)" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>
<div>
  <label form="emailinput"><b>Email</b></label>
  <input type="email" class="form-control" name="uname" id="emailinput" placeholder="[email protected]" ng-model="logcred.username" >
</div>

<div>
  <label form="pwdinput"><b>Password</b></label>
  <input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="logcred.password">
</div>

<div>
    <button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
    <button class="btn btn-primary" ng-click="submit()">Login</button>
</div>
<br/>   
</form>

AngularJS :

app.factory('auth',function($http,session) {
var authService = {};

authService.login = function(userCred) {
$http.post('http://localhost:3000/loginfo',userCred)
.then(
  function successCallback(response){
  console.log(response);
  session.create(response.data.id);
  var user = response.data;
 if (user [user.username && user.password]) {
 var userCred = user[user.username && user.password];
 if (userCred.username == user.username && userCred.password == user.password) {
    $scope.signinfo = response.data;
} else {
    console.log('Error');
  }
 }
});
};

authService.isAuthenticated = function() {
 return {
 isAuthenticated : false,
 user : null
 }
};

return authService;
});         

var app = angular.module('logpp', []);
  app.controller('credientials', function($scope, $http,auth) {
$scope.logForm = {};

$scope.userCred = {
    username : '',
    password : ''
};

    /*-----Form Submition-----*/

$scope.log = function(){
    auth.isAuthenticated = true;
    if (!$scope.logForm.$invalid) {
        $scope.login($scope.userCred);
    } else {
        console.log('error');
        return;
    }
};

    /*-----Calling Factory Function-----*/

$scope.login = function(userCred) {
    auth.isAuthenticated = true;
    auth.login(function(user) {
        console.log('success');
    },function(error) {
        console.log("error");
    });
}   

when I enter my username and password and click on login()btn , I am getting Possibly unhandled rejection in my console, I don't know why. I re-checked my code , when I submit the form , the error is getting generated from the factory function , I checked it in the console. But I am not understanding what mistake I am doing ?

Any help / advice would be greatly appreciated.

5
  • can you comment your console error. so i go through your error log Commented Oct 13, 2017 at 9:36
  • Possibly unhandled rejection: {"data":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>SyntaxError: Unexpected token # in JSON at position 0<br> &nbsp; &nbsp;at Object.parse (native)<br> &nbsp; &nbsp;at createStrictSyntaxError Commented Oct 13, 2017 at 9:38
  • should i do any parsing for my json data @NaimishMungara Commented Oct 13, 2017 at 9:39
  • if you using via model in AJ then you don't want to pass argument in form action method, simply pass method name in Action and change button type like type='submit'. try it think so it's work. Commented Oct 13, 2017 at 9:42
  • @NaimishMungara . Just now i tried but again i got the same old error Commented Oct 13, 2017 at 9:50

1 Answer 1

1

Buddy, you have declared the main app variable as myApp, and attaching factory to app variable.

var myApp = angular.module('myApp', []);


app.factory('auth',function($http,session) {
    var authService = {};

Updated answer

var myApp = angular.module('myApp', []);


myApp.factory('auth',function($http,session) {
    var authService = {};

    authService.login = function(userCred) {
        $http.post('http://localhost:3000/loginfo',userCred)
        .then(
          function successCallback(response){
              console.log(response);
              session.create(response.data.id);
              var user = response.data;
              if (user [user.username && user.password]) {
                  var userCred = user[user.username && user.password];
                  if (userCred.username == user.username && userCred.password == user.password) {
                      $scope.signinfo = response.data;
                  } else {
                      console.log('Error');
                  }
              }
          });
    };

    authService.isAuthenticated = function() {
        return {
            isAuthenticated : false,
            user : null
        }
    };

    return authService;
});         


myApp.controller('credientials', function ($scope, $http, auth) {
    $scope.logForm = {};

    $scope.userCred = {
        username: '',
        password: ''
    };

    /*-----Form Submition-----*/

    $scope.log = function () {
        auth.isAuthenticated = true;
        if (!$scope.logForm.$invalid) {
            $scope.login($scope.userCred);
        } else {
            console.log('error');
            return;
        }
    };

    /*-----Calling Factory Function-----*/

    $scope.login = function (userCred) {
        auth.isAuthenticated = true;
        auth.login(function (user) {
            console.log('success');
        }, function (error) {
            console.log("error");
        }
        );
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Actually it'a typing mistake what i made. But that's not the error
Looks there was some ;, ) was missing. can u pls check now?

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.