角$资源阵列中的数据错误阵列、错误、数据、资源

2023-09-14 00:12:40 作者:5.孤身自傲

JSON格式的服务器响应阵列数据:

Server response an array data in JSON format:

["2345","1234"]

角服务模块定义:

Angular service module define:

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

然后我用 Phone.query(); 来获取数组数据,但得到这个:

Then I use Phone.query(); to fetch array data, but got this:

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

$ HTTP

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

这是什么问题?为什么 $资源拆分阵列?请问我用 $资源错了?

What's the problem? Why $resource split the array? Does I use $resource wrong way?

感谢。

推荐答案

$资源要求的响应是对象或对象数组。要么改变你的回应是这样的:

$resource requires the response to be objects, or array of objects. Either change your response to something like this:

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

$ HTTP 服务