0

I'm a newbie to AngularJS and have a little knowledge of JavaScript.

I'm trying to implement a login page where I have a URL to my server (http://somerandomip/user/get?p=retail123&username=retail123) would give me a JSON response with dummy data upon successful login.

If the username or password is invalid, the JSON response returned will have an error message in it.

I've tried looking for tutorials to do this in the most simple way possible but I can't seem to figure out how to use $http to get this done. I understand this is pretty n00b question but my lack of JavaScript knowledge doesn't help.

If you could direct me towards a basic demo of how to get this done, I'd appreciate it a lot. Thanks in advance.

Edit: I understand that sending password as GET is not safe, I'm just trying to get a working login first.

1
  • As an advice, never send password in GET Commented Aug 18, 2014 at 4:52

3 Answers 3

1

Firstly you need to inject dependency to use $http like -

myApp.controller("myCtrl", function($scope, $http){
    ......
})

then send data via POST -

var data = {
  username: username,
  password: password 
};

Then you can POST this data to the URL -

    $http.post("/api/login", data).then(function(response){
        if (response.data) {
           // Use it accordingly with the data  
           console.log(respose.data);
        }
        else {                
            ......
        }
    })

For more information - Angular $http Docs

As a tip, explore more about Services in AngularJS.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Trialcoder, I have tried this way but so far no luck. I don't get any response whatsoever. Is it possible that CORS might have an impact on this since I'm trying to access an API in another domain? If so, how can I avoid it? Would be grateful if you could provide a basic example. None of the tutorials I found online discuss such a scenario :/
@MaheshDeSilva It would be g8 if u share some code so that I can have a look at it
1

check out this tutorial - https://medium.com/opinionated-angularjs/techniques-for-authentication-in-angularjs-applications-7bbf0346acec

Step 1 : Create an angular service(AuthService) to communicate with server and autheticate user using $http

Step 2 : use a controller to invoke the AuthService

Step 3 : Handle session timeout etc..

Comments

1

Trialcoder's solution is fine for beginners,but if u want to know sth more.

Use $httpProvider to deal all of requests and responses,and any response's header should have a new content like appStatus:997 (997 means not logged in).You can get this appStatus from backstage and deal it in public use.

In my project looks like -

$httpProvider.interceptors.push(['$q',function($q) {
  return {
    // optional method
    'request': function(config) {
      return config || $q.when(config);
    },
    // optional method
    'requestError': function(rejection) {
      return $q.reject(rejection);
    },
    response: function (response) {
      // do something on success
      var appstate = response.headers('appStatus');
      if(appstate && appstate != 200){
        if(appstate == 997){
          //997 for not logged in
          window.open('**login page url**','_self')
        }
        else if(appstate == 600){
          //600 for process error
        }
        else if(appstate == 460){
          //460 for parameters cannot be verified
        }
        else if(appstate == 560){
          //560 for unknown error
        }
        else if(appstate == 999){
          //999 for server error
        }
      }

      var reg = /.*(application\/json).*/ig;
      if(reg.test(response.headers()['content-type'])){
        /*
         //Validate response if not ok reject
         // assumes this function is available
         var data = examineJSONResponse(response);
         if(!data)
         return $q.reject(response);
         */
      }
      return response || $q.when(response);
    },
    responseError: function (response) {
      return $q.reject(response);
    }
  };
}]);

Comments

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.