【发布时间】:2015-02-12 04:25:28
【问题描述】:
更新:2015 年 2 月 12 日
如 cmets 中所述,这是由 Angular 模块修改的对象的问题。如下所述,我可以使用toJSON 追踪到这一点。
我最近遇到了一个令人难以置信的问题。我有一项服务是通过 get 请求从 api 请求数据。在 Chrome 开发人员工具中查看网络面板时,api 会返回正确的结果,但它们不会显示在角度请求的响应中。
您可以注意到内容部分缺少项目。和 Angular 代码:
角度代码
function getPhotos(){
return $http.get('/photo')
.then(getPhotosComplete)
.catch(function(err){
//handle errors
});
function getPhotosComplete(res){
//Showing incorrect response here
console.log("getPhotosComplete", res);
return res.data;
}
}
来自 API
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg",
archive_name: "140809",
seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: [
"Balloon",
"Apartment",
"Testing",
"Testing 2",
"Party",
"Inez"
],
id: "54da44790eb10b0f00b453a1"
}
Angular Scope / res.data 在服务中
{
url: "https://dev.amazonaws.com/060a2a5f-8bb7-4ffa-aa7d-e715439271a3.jpg", seedcount: 0,
viewcount: 0,
live: true,
current: false,
next: false,
createdAt: "2015-02-10T17:48:41.505Z",
updatedAt: "2015-02-12T04:11:02.239Z",
content: Array[3]
0: "Balloon",
1: "Apartment",
2: "Party"
]
【问题讨论】:
-
如果您使用的是未缩小的 Angular,您可以调试 Angular 的代码以查看 Angular 如何转换您的响应。 AFAIK,Angular 会尝试将您的响应转换为 JSON 对象。也许那里出了点问题。
-
你确定吗?尝试使用 JSON.stringify 进行日志记录,以确保它不仅仅是一个显示工件。
Array[3] 0: "Balloon", 1: "Apartment", 2: "Party" ]看起来很可疑。因为它甚至不是 JSON -
@JuanMendes - 我敢肯定,这只是控制台中返回的对象。我也使用过batarang并登录了很多地方。
-
同意@JuanMendes,尝试使用 angular.toJson() 或 JSON.stringify() 的响应记录。
-
@JuanMendes - 我用过你的方法。虽然响应中的 JSON 是正确的,但对象并未使用正确的值进行更新。对此有什么想法吗?
标签: javascript arrays json angularjs api