我在
Angularjs 1.3中有java REST服务和消费者.服务是基于JSON的,并且期望BASIC身份验证(用户名密码).
在AgJs 1.3中,我使用Base64工厂代码(在谷歌上找到)从用户名和密码创建基本身份验证令牌,并能够使用服务SUCCESSFULLY.
在AgJs 1.3中,我使用Base64工厂代码(在谷歌上找到)从用户名和密码创建基本身份验证令牌,并能够使用服务SUCCESSFULLY.
我在AgJs 2.0文档中读取了HTTP类,但是我没有找到有关基本身份验证的任何信息.
有人可以提供示例代码来使用BASIC身份验证在Angularjs 2.0中使用REST服务吗?
提前致谢!
最好的祝福!
我的AgJs 1.3代码:
var url =“http://x.x.x.x:xxxx/data/”号;
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('username' + ':' + 'password'); $http.defaults.headers.common['Content-Type'] = 'application/json'; $http({ method: 'GET',url: url }). success(function (data,status,headers,config) { alert(data); }). error(function (data,config) { alert("error: " + data); });
解决方法
我刚刚将一个从angular2发布到具有Basic身份验证的服务器,这有效:
sendAuthentification(credentials: string): Observable<any> { var headers = new Headers(); headers.append('Authorization','Basic ' + btoa('user:pass')); return this.http.get("http://ip/yourpost",headers}); }
希望这能回答你的问题.