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";
  }
 }
5
  • 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 baeldung.com/spring-security-login or if you want to write your own then you need post params stackoverflow.com/questions/2494774/… that you send from angular or a form submit. Commented Jun 16, 2019 at 4: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. Commented Jun 16, 2019 at 4:22
  • @DCTID - Thanks for your quick response. Please help me to resolve this Commented Jun 16, 2019 at 4: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. Commented Jun 16, 2019 at 6: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() } Commented Jun 16, 2019 at 8:04

0

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.