login using angular js and $http request not worki

2019-08-15 07:50发布

问题:

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 :)

回答1:

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

  • Deal with users authentication in an AngularJS web app

  • Authentication in Single Page Applications With Angular.js


Additional Resources

  • GitHub AngularJS Authentication

  • Bit of Tech: AngularJS Token Authentication

  • AngularJS User Registration and Login Example & Tutorial



回答2:

"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