Django REST API에서 데이터를 가져 오는 Angular JS로 앱을 작성하려고합니다.
Angular JS로 데이터를 가져 오지 못함
나는이 스크립트를 가지고
var userListApp = angular.module('userListApp', ['ngResource']);
userListApp.factory('Classroom', function($resource) {
return $resource('/classrooms/:id/',
{'query': {method: 'GET', isArray:false}}
);
});
userListApp.controller('UsersController', function($scope, Classroom) {
Classroom.query(function(data) {
$scope.classrooms = data;
});
});
이는 HTML
<div ng-app="userListApp">
<div ng-controller="UsersController">
<table class="table table-striped">
<thead>
<tr>
<th>Classroom</th>
<th>School</th>
<th>Academic year</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="classroom in classrooms">
<td>{{classroom.classroom}}</td>
<td>{{classroom.school}}</td>
<td>{{classroom.academic_year}}</td>
</tr>
</tbody>
</table>
</div>
</div>
입니다하지만 콘솔
Error: $resource:badcfg
Response does not match configured parameter
Error in resource configuration for actionquery
. Expected response to contain an array but got an object (Request: GET /classrooms)
(from the error reference page of Angular JS)
에서이 오류를 받고 있어요여기에 유래에 대한 몇 가지 답변을보고하고, this one에 와서, 그리고 나는 그들이
userListApp.factory('Classroom', function($resource) {
return $resource('/classrooms/:id/',
{method: 'classrooms', id: '*'},
{'query': {method: 'GET', isArray:false}}
);
});
을 말하려고 노력 그리고 나는 어떤 오류가 발생하지 않습니다,하지만 데이터 중 하나있어했다.
나는이를 넣어 시도했다 : 뭔가 "캐치"를하려고하면 같은 웹 페이지에 일부 테이블은 여기 UPDATE가
스크린 샷
이다, 그러나이있다 대신이
라인return $resource('/classrooms/?format=json',
각 응용 프로그램에
, 내가 네트워크에서 개발자 도구에 가면,이 데이터가 없지만 -> XHR, 내가 얻을 말했듯
[{"school":{"id":1,"school_name":"IPSIA F. Lampertico","address":"Viale Giangiorgio Trissino, 30","city":"Vicenza"},"academic_year":"2015/2016","classroom":"1^A","floor":0,"students":[{"classroom":1,"first_name":"Stefano","last_name":"Rossi","gender":"M","birthday":"1998-06-22"},{"classroom":1,"first_name":"Luca","last_name":"Possanzini","gender":"M","birthday":"1999-11-22"}]
는하지만, 데이터가없는 JSON 데이터 html의 표에 나와 있습니다.
업데이트 2 : "형식 = JSON '"추가 한 후
자원 URL에, 나는 내가 세 교실이 내 데이터 (행의 정확한 양을 얻을, 그래서 나는 세 개의 행을 얻을 HTML 테이블에서), 데이터가 없습니다.
UPDATE 3 :
내가 콘솔 로그
음, 스크린 샷은 테이블이 많은 행이 있기 때문에 당신이 데이터를 가지고 할 수 있음을 보여준다. 그것들은 비어 있습니다. 즉, 당신이 가진 일은 교실, 학교 및 academic_year라는 이름의 필드가 없거나 비어 있습니다. 브라우저의 dev 도구를 열고 실제로 얻은 것을보십시오. –