Angular promise从JSON API返回不正确的数据

Angular promise returning incorrect data from JSON API

本文关键字:不正确 数据 返回 API promise JSON Angular      更新时间:2023-09-26

更新时间:2015年12月2日正如评论中提到的,这是一个由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.toJson()的输出是正确的,但对象的console.log是错误的,则强烈建议在调用console.log之后的某个地方修改代码中的对象。

这是因为console.log引用了一个对象,一段时间后它将其转换为一个字符串进行输出。我会把它归类为轻微的邪恶行为,因为它表明错误在调用console.log之前,而实际上是在调用之后的问题。查看错误的代码可能会浪费几个小时。。。