AngularJS控制器不与Spring控制器通信

时间:2016-08-11 15:50:59

标签: java angularjs spring rest spring-boot

我正在尝试使用通过使用AngularJS控制器使用Spring创建的REST API。我想做的就是使用AngularJS将表单中的凭据发送到后端的Spring控制器;但是,他们似乎根本没有沟通。每当我按下登录按钮时,页面都会刷新。我将记录器放在我试图调用的Spring控制器中,但它们从不打印任何东西,这意味着控制器永远不会被调用。

AngularJS控制器:

angular.module('landing', [ 'ngRoute' ])
        .controller('authentication',
          function($rootScope, $scope, $http, $location) {
          $scope.credentials = {};
          $scope.login = function() {
            $http.post('http://localhost:8090/login', $.param($scope.credentials), {
              headers : {
                "content-type" : "application/json"
              }
            }
          };
        });

我试图利用的Spring控制器:

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<BusinessUser> login(@RequestBody BusinessUser inputUser) {
        logger.info(inputUser.getUsername());
        logger.info(inputUser.getPassword());
        BusinessUser requestedUser = businessUserService.findByUsername(inputUser.getUsername());
        if(requestedUser != null)
            if(BCrypt.checkpw(inputUser.getPassword(), requestedUser.getPassword()))
                return new ResponseEntity<>(requestedUser, HttpStatus.OK);
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

最后,这是表格:

<body data-spy="scroll" data-target=".navbar" ng-app="landing">
   <div id="home" class="border-shadow">
        <div class="container-fluid container-background-color">
            <div class="row border-shadow">
                <div class="col-lg-7">
                    <h2 class="heading-margin main-color"><img class="imagesize" src="images/icon.png"/><b>Test Company</b></h2>
                </div>
                <div class="col-lg-5" ng-controller="authentication">
                    <form role="form" ng-submit="login()">              
                        <div class="form-group row row-margin">
                            <div class="col-lg-5">
                                <input type="text" class="form-control" ng-model="credentials.username" placeholder="Username" required>
                            </div>
                            <div class="col-lg-5">       
                                <input type="password" class="form-control" ng-model="credentials.password" placeholder="Password" required>
                                <a href="#" class="forget"> Forgot Your Password</a>
                            </div>
                            <div class="col-lg-2">
                                <button type="submit" class="btn btn-success">Log in</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

我可以使用名为“Postman”的Chrome添加内容发送请求,结果完美无缺,因此这是一个AngularJS问题。知道什么是错的吗?

编辑顺便说一下,这是一个Spring Boot项目。

0 个答案:

没有答案