我对AngularJS的$ http有一个奇怪的行为,并没有真正理解transformResponse是如何工作的(文档在这一点上有点亮).
WebAssets.get = function () {
return $http.get('/api/webassets/list', {
transformResponse: [function (data, headersGetter) {
// not sure what to do here?!
return data;
}].concat($http.defaults.transformResponse) // presume this isn't needed, added for clarity
}).then(function (response) {
return new WebAssets(response.data);
});
};
Run Code Online (Sandbox Code Playgroud)
api返回一个对象数组:
[{"webasset_name": "...", "application_id": "...", "etc": "..."}, ... ]
Run Code Online (Sandbox Code Playgroud)
但是当transformResponse做了它的恶行时,数据已经变成了一个索引对象:
{"0":{"webasset_name":"...","application_id":"...", "etc": "..."}, "1":....}
Run Code Online (Sandbox Code Playgroud)
我想保留原始数据结构(对象数组).