登录采用了棱角分明的js和$ http请求不工作 [英] login using angular js and $http request not working
问题描述
我想用 $ HTTP
和 GET
的 REST Web服务登录code>。什么我做的是:
- 以用户名的基础上,数据并使用密码$ http.get
- 保存结果:
$ scope.getResult =资源;
-
在某些变量保存它:
VAR的结果= $ scope.getResult;
VAR用户名= result.userName;结果
VAR密码= result.password;结果
VAR用户= this.user;
VAR通= this.pass; -
之后,我将与用户名和密码的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
<一个href=\"http://frederiknakstad.com/2013/01/21/authentication-in-single-page-applications-with-angular-js/\"相对=nofollow>认证的单页应用随着Angular.js
其他资源
<一个href=\"http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/\"相对=nofollow>高科位:AngularJS令牌认证
I want to login using
$http
andGET
ofREST web services
. What I am doing is:
- Taking data on the basis of username and password using $http.get
- Storing the result:
$scope.getResult=res;
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;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. Callingwindow.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屋!