登录采用了棱角分明的js和$ http请求不工作 [英] login using angular js and $http request not working

查看:206
本文介绍了登录采用了棱角分明的js和$ http请求不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 $ HTTP GET REST Web服务。什么我做的是:


  1. 以用户名的基础上,数据并使用密码$ http.get

  2. 保存结果: $ scope.getResult =资源;

  3. 在某些变量保存它:

    VAR的结果= $ scope.getResult;
    VAR用户名= result.userName;结果
    VAR密码= result.password;结果
    VAR用户= this.user;
    VAR通= this.pass;


  4. 之后,我将与用户名和密码的NG模型进行比较。如果它是成功的,那么它会重定向到另一个页面如下图所示:

    如果(用户名==用户放大器;&放大器;密码==通)
     {
        window.location的=/ next.html;结果
      }


下面是我的完整code和建议,请在我的情况需要改变。

  $ scope.login =功能(){  VAR用户= this.user; //在html页面的用户名的NG-模型
  VAR通= this.pass; //在html页面密码NG-模型  $ http.get(HTTP://本地主机:17681 / API / userRegisters+用户+通).success(功能(RES){    $ scope.getResult =资源;    VAR的结果= $ scope.getResult;
    VAR用户名= result.userName;
    VAR密码= result.password;    如果(用户名==用户放大器;&放大器;密码==通){
      window.location的=/ next.html;
    }  }}

请告诉我,我应该怎么改变我的code,为了做到这一点。

在此先感谢:)


解决方案

与AngularJS $位置 window.location的方法code>服务。调用 window.location的从里面 $ Q 业务开通处理器通常会导致AngularJS内部错误。

  $ http.get(HTTP://本地主机:17681 / API / userRegisters+用户+通)
  。然后(功能onFulfilled(响应){    $ scope.getResult = response.data;    VAR的结果= $ scope.getResult;
    VAR用户名= result.userName;
    VAR密码= result.password;    如果(用户名==用户放大器;&放大器;密码==通){
      //用这个
      $ location.path(/ next.html);
      //不是这个
      //window.location=\"/next.html;
    }
})赶上(功能onRejected(响应){
    的console.log(response.status);
});

有关详细信息,请参阅 AngularJS $位置服务API参考


  

德precation通知


  
  

$ HTTP 遗留承诺的方法 .success .error 已去precated。使用标准的。然后方法来代替。 1



调试响应


  

其通过response.data给人的纪录,但犯规出栏记录。又该以获取数据怎么办?


什么是服务器端API规范?

什么是服务器端的code生成响应数据?

这些都是读者需要,以帮助您就知道了。否则,任何答案只是一个猜测。

行业最好的做法是避免发送密码一样有安全问题的URL的参数。

由AngularJS团队推荐两种资源: 2


其他资源

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

解决方案

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

这篇关于登录采用了棱角分明的js和$ http请求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆