我想通過HTTP獲取方法從這個網站獲取數據。這個網站有基本的認證。數據採用JSON格式。 這是REST API網站: (https://shoploapi.herokuapp.com/sellers)如何從具有基本身份驗證的rest api獲取數據?
用戶名: 「bhupendra7」 &密碼: 「ice123age456」
// Code goes here
angular.module('myapp', ['myapp.controller']);
angular.module('myapp.controller', ['myapp.service'])
.controller('testController', function($scope, testService) {
$scope.posts = {};
function GetAllPosts() {
var getPostsData = testService.getPosts();
getPostsData.then(function(post) {
$scope.posts = post.data;
}, function() {
alert('Error in getting post records');
});
}
GetAllPosts();
});
angular.module('myapp.service', [])
.service('testService', function($http) {
//get All NewsLetter
this.getPosts = function() {
return $http.get('https://shoploapi.herokuapp.com/sellers');
};
});
angular.module('myApp', ['base64'])
.config(function($httpProvider, $base64) {
var auth = $base64.encode("bhupendra7:ice123age456");
$httpProvider.defaults.headers.common['Authorization'] = 'Basic ' + auth;
});
<!DOCTYPE html>
<html>
<head>
<script data-require="[email protected]" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-base64/2.0.5/angular-base64.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body ng-app="myapp">
<h1>Hello Plunker!</h1>
<div ng-controller="testController">
<div>
<ul>
<li ng-repeat="post in posts">
{{post.careof}} {{post.district}} {{post.gender}} {{post.name}}
</li>
</ul>
</div>
</div>
</body>
</html>
這裏的鏈接到我的Plunker:
(https://plnkr.co/edit/7pqljm?p=preview)
任何人都可以幫忙嗎?
你有'angular.module一個錯字( '對myApp',[ '的base64'])''做對myApp''myapp'。即使這是正確的,你將面臨另一個問題,testController是「未定義」。讓我知道如果你需要更多的幫助 –