1

I want to login using $http and GET of REST web services. What I am doing is:

  1. Taking data on the basis of username and password using $http.get
  2. Storing the result: $scope.getResult=res;
  3. Storing it in some variable:

    var result=$scope.getResult; var username=result.userName;
    var password=result.password;
    var user=this.user; var pass=this.pass;

  4. After that I will compare with the ng-model of username and password. If it is successful then it will redirect to another page like below:

    if (username == user && password == pass) { window.location="/next.html";
    }

Below is my complete code and please suggest the changes required in my case.

$scope.login = function () {

  var user = this.user ; // ng-model of username in html page
  var pass = this.pass; //ng-model of password in html page  

  $http.get("http://localhost:17681/api/userRegisters"+user+pass).success(function (res) {

    $scope.getResult = res;

    var result = $scope.getResult;
    var username = result.userName;
    var password = result.password;

    if (username == user && password == pass) {
      window.location="/next.html";
    }

  }

} 

Please tell me how should I change my code in order to do that.

Thanks in advance :)

8
  • 3
    your url will be "http://localhost:17681/api/userRegistersusernamepassword", put in some slashes Commented Feb 22, 2016 at 10:09
  • can you log the value of user and pass and see if the user and pass is actually there. Plus what you are doing with the url is sending a concatinated string of user and pass i.e. /userRegistersapplepassword. I don't think your api is expecting that. Commented Feb 22, 2016 at 10:10
  • Sorry , i am new to angularjs and i am not getting how to place filter in http url to compare the username in the database with the username entered Commented Feb 22, 2016 at 10:19
  • Also, when sending forms, especially with passwords, you should send them in a $http.post request instead of a get Commented Feb 22, 2016 at 10:30
  • thank you @devqon but i am not able to get you . can post some example or links where i can study that ? It would be very helpful. Commented Feb 22, 2016 at 10:36

2 Answers 2

2

Replace the raw window.location method with the AngularJS $location service. Calling window.location from inside a $q service fulfillment handler will generally result in an AngularJS internal error.

$http.get("http://localhost:17681/api/userRegisters"+user+pass)
  .then(function onFulfilled(response) {

    $scope.getResult = response.data;

    var result = $scope.getResult;
    var username = result.userName;
    var password = result.password;

    if (username == user && password == pass) {
      //Use this
      $location.path("/next.html");
      //Not this
      //window.location="/next.html";
    }
}).catch ( function onRejected(response) {
    console.log(response.status);
});

For more information, see AngularJS $location Service API Reference.

Deprecation Notice

The $http legacy promise methods .success and .error have been deprecated. Use the standard .then method instead.1


Debugging the Response

Its gives record via response.data but doesnt give column records. what should be done in order to fetch data ?

What is the specification for the server side API?

What is the server side code that generates the response data?

These are things readers will need to know in order to help you. Otherwise any answer is just a guess.

Industry best practice is to avoid sending passwords as a parameter of a URL as that has security issues.

Two resources recommended by the AngularJS team:2


Additional Resources

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

2 Comments

thank you very much @georgeawg sir :) but when i alert username and password variable it doesnt show the username and password record from the Db table . Its gives record via response.data but doesnt give column records. what should be done in order to fetch data ?
Added section on debugging the response. And added resource links for authentication.
0

"http://localhost:17681/api/userRegisters?username= " + username + "&password=" + password

and read the username and password from http://localhost:17681/api/userRegisters this location by using GET HTTP METHOD

3 Comments

I would recommend a POST instead and send the data in the body. Bad practice to put passwords in a url
Niyaz Thanks for the reply but this script is showing the whole database and not just for the given username
Yeah i agrre with the POST as it contains pssword , and i think the url doesnt exists... what the error says when u call ajax $http.get("localhost:17681/api/… (err) { console.log(err); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.