0

I'm new to angular with rest api. Simple login page to home page navigation with java , rest api, with angular js 1. But on click of login button it redirects to same login page. But I could see logs and no error.

This is my app.js

   var loginapp = angular.module('loginapp' ,['ngRoute','ngResource']);
   loginapp.config(['$routeProvider', function($routeProvider) {
    console.log("config");
    $routeProvider
     .when('/', {
        templateUrl: 'login.html',
        controller: 'loginController'
     })
    .when('generate-report', {
        templateUrl: 'generate-report.html',
        controller: 'generatexml'
    })
    .otherwise({
        redirectTo: '/generate-report'
    });
    }]);

    loginapp.controller('loginController',['$scope', '$http', function 
  ($scope,$http) {

  $scope.submitlogin = function (response) {
     console.log("submit login called")
     $http({
         method : 'POST',
         url : 'login',
         data : '',
         headers : {
             'Content-Type' : 'application/json'
         }
     }).then( response );
  }
 }]);

my html code

<body ng-app="loginapp">
 <div class="login-box" ng-controller="loginController">
  <form>
     <input id="inputUserId" type="text" >
     <input id="inputPassword" type="password" >
     <button  ng-click="submitlogin()" >Sign in</button>
  </form>
 </div>
</body>

This is my java controller class @Controller public class LoginController {

 @RequestMapping(value="/" , method = RequestMethod.GET)
public String viewLogin(Model model) {
    System.out.println("login controller");
    return "login";
 }

  @RequestMapping(value="/login", method = RequestMethod.POST)
  public String submitLogin() {
     System.out.println("Login controller - submitLogin");
    return "generate-report";
  }
 }
hary
  • 1
  • 1
  • Your login controller doesn't take in any params, such as username and password. This looks like a spring boot app, you can just use the spring security you don't have to write your own https://www.baeldung.com/spring-security-login or if you want to write your own then you need post params https://stackoverflow.com/questions/2494774/how-to-explicitly-obtain-post-data-in-spring-mvc that you send from angular or a form submit. – DCTID Jun 16 '19 at 04:05
  • now i did not start, passing input params and other security validations. Just I want to see, click on submit button, and navigates to another page. – hary Jun 16 '19 at 04:22
  • @DCTID - Thanks for your quick response. Please help me to resolve this – hary Jun 16 '19 at 04:28
  • can you log the ````response```` of the login API and show it here. I also don't see navigation being done anywhere in your code. You need to navigate to the ````generate-report```` route after you get your ````response```` in the ````.then()```` block. – nash11 Jun 16 '19 at 06:11
  • .then( response); function _success(response) { $http({ method : 'GET', url : 'generate-report', }).then(function successCallback(response) { console.log("success") }, function errorCallback(response) { console.log("error") }); ///_clearForm() } – hary Jun 16 '19 at 08:04

0 Answers0