简体   繁体   中英

how to authenticate with url in angularjs

 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="myCtrl"> <h1>{{myWelcome}}</h1> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $http,Base64) { $http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; $http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('admin' + ':' + 'admin'); $http({ method : "GET", url : "http://10.132.32.212:8181/restconf/operational/network-topology:network-topology/", Accept: "application/json", withCredentials: true, }).then(function mySucces(response) { $scope.myWelcome = response.data; }, function myError(response) { $scope.myWelcome = response.statusText; }); }); </script> </body> </html> 

this is my code but i am not getting any response data .i am thinking that it is not authenticating with the url. can i get any solution

You used an undefined Base64 service. I added it as a CDN library.

see

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-base64/2.0.5/angular-base64.js"></script>

and the inclusion of the service in the app declaration:

var app = angular.module('myApp', ['base64']);

results in

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-base64/2.0.5/angular-base64.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl"> 



<h1>{{myWelcome}}</h1>

</div>

<script>
var app = angular.module('myApp', ['base64']);
app.controller('myCtrl', function($scope, $http, $base64) {
$http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; 
$http.defaults.headers.common['Authorization'] = 'Basic ' + $base64.encode('admin' + ':' + 'admin');
$http({ method : "GET",
    url : "http://10.132.32.212:8181/restconf/operational/network-topology:network-topology/",
    Accept: "application/json",
    withCredentials: true,
    }).then(function mySucces(response) {
      $scope.myWelcome = response.data;
    }, function myError(response) {
      $scope.myWelcome = response.statusText;
  });
});
</script>
</body>
</html>

Plunker: https://plnkr.co/edit/eYJopsNPe8Iqb56arC75?p=preview

Load this code from your server ( http://10.132.32.212:8181 ) and you're good to go.

 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-base64/2.0.5/angular-base64.js"></script> <script type="text/javascript" src="../../dist/vis.js"></script> <body> <div ng-app="list"> <div ng-controller="ListCtrl"> <div ng-repeat='data1 in data["network-topology"].topology'> {{data1["topology-id"]}} <div ng-repeat='data2 in data1.node'> {{data2["node-id"]}} <div ng-repeat='data3 in data2["termination-point"]'> {{data3["tp-id"]}} </div> </div> </div> </div> </div> <script> var app = angular.module('list', ['base64']); app.controller('ListCtrl', function($scope, $http, $base64) { $http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; $http.defaults.headers.common['Authorization'] = 'Basic ' + $base64.encode('admin' + ':' + 'admin'); $http({ method: 'GET', url: 'http://10.132.32.212:8181/restconf/operational/network-topology:network-topology/', contentType: 'application/json; charset=utf-8', }).success(function(tdata) { $scope.data= tdata; }); }); </script> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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