2015-05-15 40 views
-2

我想從我的Django本地服務器獲取HTTP響應,它返回一個JSON響應。

JSON響應:

[{"fields": {"DOB": "2015-05-08", "image": "media/jainsubh_1394437734_6.png", "text_file": "my_Txt_Files/JN2i0oq.png", "usr_name": "ankitmishra", "email": "[email protected]"}, "model": "my_fr_app.new_user_reg", "pk": 15}] 

角代碼:

<!DOCTYPE html> 
<html> 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
<body> 

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

<ul> 
    <li ng-repeat="x in items"> 
    {{ x.usr_name }} 
    </li> 
</ul> 

</div> 

<script> 
var app = angular.module('myApp', []); 
app.controller('customersCtrl', ['$http',function($http) { 
    $http.get("http://127.0.0.1:8000/members") 
    .success(function (response) { 
    this.items = response.data.fields; 
    }]); 
}); 
</script> 

</body> 
</html> 

我不知道爲什麼它的成功回調不執行!儘管服務器返回響應代碼200 & 304表示獲取請求正在執行。

+0

你說的 '成功回調不執行' 是什麼意思?怎麼了? – 0xc0de

+0

*「我不知道爲什麼它的成功回調沒有執行」*你如何得出這個結論?你添加了一個console.log嗎?你檢查網絡選項卡上的CORS錯誤嗎? –

回答

-1

只是發佈一個替代品,爲了好玩,你也可以在Angular中使用ControllerAs功能來做到這一點。

<div ng-app="myApp" ng-controller="customersCtrl as vm"> 

<ul> 
    <li ng-repeat="x in vm.items"> 
    {{ x.usr_name }} 
    </li> 
</ul> 

</div> 

<script> 
    var app = angular.module('myApp', []); 
    app.controller('customersCtrl', ['$http', '$scope',function($http, $scope) { 
     var vm = this; // this is to help you with closure issues. Using the ControllerAs functionality, there is no need to go into $scope at all. 
     $http.get("http://127.0.0.1:8000/members") 
      .success(function (response) { 
       vm.items = response.data.fields; 
     }); 
    }]); 
</script> 

http://toddmotto.com/digging-into-angulars-controller-as-syntax/

+1

爲什麼downvote沒有評論?不具有建設性,並且上述確實按預期工作。 – KnowHowSolutions