0

I am trying to create a login page where the log in credential is checked in Backend and returns data... So I am using angular js for front end.

When I make a restful API call, it checks for the login credential but i am not getting the data back from the backend.

Ajax call to my API returns undefined so I am not able to display the response.data

Controller: angular.module('myApp')

.controller('LoginCtrl', function ($scope, $location, LoginService) {

    $scope.loginData = {};
    $scope.errors = [];
    this.msg={};
    $scope.login = function () {
    if ($scope.loginData != null) {
        console.log('Login Controller');
        console.log($scope.loginData);
        LoginService.Login($scope.loginData).then(function (response) {
            console.log('Login Controller2');
            $scope.showHide="IsVisible";
            $scope.msg=response.data;
            $location.path('signIn');              
            $scope.statuscode = response.status;
            console.log(response.status);
            console.log(response.headers);
            console.log(response.config);
            console.log(response.statusText);
            console.log(response.xhrStatus);
            //console.log(statuscode);
            //console.log(msg.message);
           console.log(response.data);
           var result = response.resource;        
          // result.$status = response.status;
         //  console.log(result.$status);


        }, function (reason) {
           // $scope.errors.push(reason.data.toString());
        });

Service: angular.module('myApp') .factory('LoginService', ['$http', function ($http) {

    return {

        Login: function(loginData){
            console.log('Login Service');
            console.log(loginData.password);
            return $http.get('api/userlogin/checklogin', {params: {username: loginData.username, password:loginData.password}}) .then(testSuccess)
            .catch(testError);
            function testSuccess(response){
                console.log('Login Successful');
                console.log(response.data);
                return response.data;

                //console.log(response.data) this line prints data to console
             }

             function testError(error){
                return error;
             }

        },

How can I get the data from the server and display the data in html?

Ben
  • 1
  • Possible duplicate of [Processing $http response in service](https://stackoverflow.com/questions/12505760/processing-http-response-in-service) – Pop-A-Stash Jan 23 '19 at 19:57
  • Are you sure the request gets to the back-end? Maybe it doesn't get to the right endpoint (404 maybe?) or you're not sending a response correctly.. You should check the back-end's logs to see what happens, and use the Chrome Developer Tools to see if the request is sent correctly (and what response it gets (if any)). –  Jan 23 '19 at 22:19
  • Yes, the request hits the backend well. and for now I am just trying to return a string like successful or fail..But when i use chrome developer tool to see the response by using response.data..it says undefined there. – Ben Jan 23 '19 at 23:28
  • @RequestMapping(value="/checklogin", method=RequestMethod.GET) @ResponseBody public String checkname(@RequestParam("username") String username, @RequestParam("password") String password ) { System.out.print(username); System.out.print(password); if(username.equals("Test") && password.equals("123")) { System.out.print("Login Success"); return "Login Success"; } else { System.out.print("Login Failed"); return "Login Failed"; } } – Ben Jan 23 '19 at 23:29
  • The code in comment above does not give us any understanding of what's going on on you back-end side, but there is nothing wrong with AngularJS code from your question, could you tell us what is in Chrome DevTools request `response` section? – Majesty Jan 24 '19 at 06:18
  • Request URL: http://localhost:8080/api/userlogin/checklogin?password=123&username=Test Request Method: GET Status Code: 200 Remote Address: [::1]:8080 Referrer Policy: no-referrer-when-downgrade Content-Length: 13 Content-Type: application/json;charset=UTF-8 Date: Thu, 24 Jan 2019 17:07:43 GMT Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Connection: keep-alive Cookie: __ngDebug=false Host: localhost:8080 Referer: http://localhost:8080/ – Ben Jan 24 '19 at 17:13
  • Response Section: Login Success It looks good in chrome DevTools request response section but somehow I am not able to get the data or status code in my view – Ben Jan 24 '19 at 17:13
  • I just realized that it works totally fine when I return an object from server but when I try to return String such as "Success " or "Fail", it shows bad data error – Ben Jan 28 '19 at 02:25

0 Answers0