2017-03-03 82 views
0

如何使用URL驗證在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>

這是我的代碼,但我沒有得到任何響應數據。我在想,這是不是與URL驗證。我可以得到任何解決方案

+0

是什麼反應?你有401狀態碼嗎? – mvermand

+0

不,我沒有得到任何東西,它只顯示{{我的歡迎}}表達式 –

+0

如果您實際在html頁面中看到「{{myWelcome}}」,那麼存在嚴重錯誤。檢查CONSOL錯誤 – mvermand

回答

0

您使用了未定義的Base64服務。 我將它添加爲CDN庫。

看到

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

,並在應用程序申報列入服務:

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

結果

<!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

大號從你的服務器(http://10.132.32.212:8181)刪除這段代碼,你很好。

+0

感謝您的支持,但現在我也無法獲取數據。空瀏覽器正在顯示 –

+0

您的意思是在Plunker中嗎?這是由於腳本不是從'你的'服務器(http://10.132.32.212:8181) – mvermand

+0

加載哎抱歉它的工作 –

0

<!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>