Angular $ Resource Array Data Error

Server response to a data array in JSON format:

["2345","1234"] 

Angular service module defines:

 angular.module('MySource', ['ngResource']).factory('Phone', function($resource){ return $resource('/api/source'); }); 

Then I use Phone.query(); to retrieve the array data, but got the following:

 [{"0":"2","1":"3","2":"4","3":"5"},{"0":"1","1":"2","2":"3","3":"4"}] 

But $http works:

 $http.get('/inner/source').success(function(data){ // data = ["2345", "1234"] }); 

What is the problem? Why is $resource sharing an array? Am I using $resource wrong way?

Thanks.

+4
source share
1 answer

$resource requires the response to be an object or an array of objects. Or change your answer to something like this:

 [{"value":"2345"},{"value":"1234"}] 

or use the $http service

+4
source

Source: https://habr.com/ru/post/1483949/


All Articles