我正試圖學習在AngularJS中使用$ http.get請求來創建我正在編寫的Web應用程序。我有一個本地託管的服務器,它具有我編寫的一些API。當我嘗試運行我的Angular頁面時,我可以在服務器的控制檯中看到GET請求已完成,但瀏覽器中沒有加載。在檢查瀏覽器的控制檯我發現此錯誤消息:
XMLHttpRequest cannot load http://127.0.0.1:8080/api/range/?nmax=5&a_max=100&b_max=200, No 'Access-Control-Allow-Origin' header is print on the requested resource. Origin 'null' is therefore not allowed access.
服務器端我正在運行,將採取一個GET請求以上網址,並返回一個JSON字符串看起來像這樣的Python的CherryPy服務器:
[
{
"a": 5.6,
"b": 3.2
},
{
"a": 4.3,
"b": 2.6
}
]
這裏是我的角碼,此刻它儘可能簡單:
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="Ctrl">
<ul>
<li ng-repeat="x in peakdata">
{{ x.a + ', ' + x.b }}
</li>
</ul>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('Ctrl', function($scope, $http) {
$http.get('http://127.0.0.1:1234/api/range/?nmax=5&a_max=100&b_max=200')
.then(function(response){
$scope.peakdata = response.data;
}, function(err) {
throw err;
});
});
</script>
</body>
</html>
任何建議,將不勝感激!
謝謝,肖恩。
編輯:原來,我不得不修改我的AngularJS代碼和我的CherryPy腳本。查看我的答案瞭解更多詳情。
雖然這個問題沒有回答我的問題,它確實提供了我需要谷歌關鍵詞和術語,我需要的答案,我正在尋找。非常感謝你,我會發佈一個答案,詳細說明我是如何解決我的問題的。 – Vielbein