简体   繁体   English

使用angular js和$ http请求登录不起作用

[英]login using angular js and $http request not working

I want to login using $http and GET of REST web services . 我想使用$httpREST web services GET登录。 What I am doing is: 我正在做的是:

  1. Taking data on the basis of username and password using $http.get 使用$ http.get根据用户名和密码获取数据
  2. Storing the result: $scope.getResult=res; 存储结果: $scope.getResult=res;
  3. Storing it in some variable: 将其存储在一些变量中:

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

  4. After that I will compare with the ng-model of username and password. 之后,我将与用户名和密码的ng模型进行比较。 If it is successful then it will redirect to another page like below: 如果成功,它将重定向到另一个页面,如下所示:

    if (username == user && password == pass) { window.location="/next.html"; 如果(用户名==用户&&密码==通过){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 :) 提前致谢 :)

Replace the raw window.location method with the AngularJS $location service. 将原始window.location方法替换为AngularJS $location服务。 Calling window.location from inside a $q service fulfillment handler will generally result in an AngularJS internal error. $q服务实现处理程序内部调用window.location通常会导致AngularJS内部错误。

$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 . 有关更多信息,请参见《 AngularJS $ location服务API参考》

Deprecation Notice 弃用通知

The $http legacy promise methods .success and .error have been deprecated. $http旧版.success方法.success.error已过时。 Use the standard .then method instead. 请使用标准的.then方法。 1 1


Debugging the Response 调试响应

Its gives record via response.data but doesnt give column records. 它通过response.data提供记录,但不提供列记录。 what should be done in order to fetch data ? 为了获取数据应该怎么做?

What is the specification for the server side API? 服务器端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. 行业最佳实践是避免将密码作为URL的参数发送,因为这样做存在安全问题。

Two resources recommended by the AngularJS team: 2 AngularJS团队推荐的两个资源: 2


Additional Resources 其他资源

" http://localhost:17681/api/userRegisters?username= " + username + "&password=" + password http:// localhost:17681 / api / userRegisters?username = ” +用户名+“& pass = ” =“ +密码

and read the username and password from http://localhost:17681/api/userRegisters this location by using GET HTTP METHOD 并从http:// localhost:17681 / api / user中读取用户名和密码使用GET HTTP METHOD 注册此位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM