我編寫了一個REST API,並且the api在瀏覽器中正常工作。但是我無法從AngularJS中獲取數據。我的代碼如下。未能在AngularJS中獲得json url
這是在瀏覽器中的服務器響應:
[{ 「ID」:1, 「稱號」: 「AAA」, 「內容」:空},{ 「ID」:2, 「標題」日期null, 「內容」:空},{ 「ID」:3 「標題」:空, 「內容」:空},{ 「ID」:4 「標題」:空, 「內容」:空} { 「ID」:5 「標題」:空, 「內容」:空},{ 「ID」:6中, 「標題」:空, 「內容」:空},{ 「ID」:7, 「標題」日期null, 「內容」:空},{ 「ID」:8中, 「標題」:空, 「內容」:空},{ 「ID」:9 「標題」:空, 「內容」:空} { 「ID」:10, 「標題」:空, 「內容」:空},{ 「ID」:11, 「標題」:空, 「內容」:空},{ 「ID」:12, 「標題」 :null,「content」:null},{「id」:13,「title」:null,「content」:null}]
朋友們: 謝謝大家,現在我知道有一個跨域問題在這裏。但我不確定爲什麼我仍然可以從鉻合金中獲得它?如果我可以在服務器端的響應中添加一些標題,這會起作用嗎?
angular.module('job',[])
.controller('BlogController',['$http',function($http){
this.LoadDate = function(){
$http.get("http://alvin-api.herokuapp.com/application/skills")
.success(function(response) {
alert("success");
}).error(function(response){
alert("error");
});
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="job">
{{1+3}}
<div ng-controller="BlogController as b">
<input type='button' value = 'click me' ng-click="b.LoadDate()"/>
</div>
</div>
你可以用當前的服務器響應進行編輯嗎?什麼是狀態碼等... – AdamCooper86
嗨,我編輯了我的問題並添加了服務器響應數據 – SmartFingers
閱讀關於CORS。 – dfsq