Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Service implementation in angularjs not working

This is my controller which is calling the login service

mod.controller("loginCtrl",function($scope,loginService,$http)
{
      $scope.Userlogin = function()
      {
          var User = {
              userid :$scope.uname,
              pass:$scope.pass
          };

          var res = UserloginService(User);

          console.log(res);
          alert("login_succ");
      }          
});

And this is the login service code which takes the User variable and checks for username & password

mod.service("loginService",function($http,$q) {
    UserloginService = function(User) {

    var deffered = $q.defer();
    $http({
        method:'POST',
        url:'http://localhost:8080/WebApplication4_1/login.htm',
        data:User
    }).then(function(data) {
        deffered.resolve(data);
    }).error(function(status) {
        deffered.reject({
            status:status
        });
    });
    return deffered.promise;

    // var response = $http({
    //
    //   method:"post",
    //   url:"http://localhost:8080/WebApplication4_1/login.htm",
    //   data:JSON.stringify(User),
    //   dataType:"json"
    // });

    //  return "Name";
  }
});

I have created a rest api using springs which upon passing json return back the username and password in json like this

Postman screenshot

Console shows me this error for angular Error in console

like image 870
ashu gupta Avatar asked Feb 28 '26 14:02

ashu gupta


1 Answers

You need to enable CORS for your application for guidance see this link https://htet101.wordpress.com/2014/01/22/cors-with-angularjs-and-spring-rest/

like image 82
Kanya Ravi Avatar answered Mar 03 '26 05:03

Kanya Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!