0

我正在尝试从 Wattpad.com 的 API 级别 3 http://www.wattpad.com/api/v3/stories/ 获取,使用

服务

angular.module('myApp')
  .service('WattpadService', function WattpadService($resource) {
      var resource = $resource('http://www.wattpad.com/api/v3/stories', {}, {
        query: {method:'GET',
                params:{query:'margaretatwood'},
                isArray:false
                 }   
      }); 
      return {
        loadStories: function() {
          return resource.query();
        }   
      };  
  }); 

控制器

angular.module('myApp')
  .controller('MainCtrl', function ($scope, WattpadService, $resource) {
    WattpadService.loadStories()
    .$promise.then(
      function(data) {
        console.log("success");
        console.log(data);
      },  
      function(response) {
        console.log(response);
      }   
    );  

});

我得到的数据来自 console.log 非常奇怪:

Resource
0: "a"
1: "r"
2: "r"
3: "a"
4: "y"
5: " "
6: "("
7: "↵"
(etc)

据我所知,我遇到了http://mariuszprzydatek.com/2013/12/13/tricky-behavior-of-angularjs-resource-service/中描述的问题。使用 transformResponse 像:

query: {method:'GET',
    params:{query:'margaretatwood'},
    isArray:false,
    transformResponse: function(data, headers){
    return JSON.parse(data);
    }
} 

得到我

SyntaxError: Unexpected token a
    at Object.parse (native)

有什么建议吗?

在解析数据之前编辑console.log (使用只有 1 个故事的随机用户,而不是有很多故事的 Atwood。)

array (
  'stories' => 
  array (
    0 => 
    array (
      'id' => '18265912',
      'title' => 'A Little Something Different',
      'length' => 9370,
      'createDate' => '2014-06-23T21:48:55Z',
      'modifyDate' => '2014-07-15T18:24:45Z',
      'voteCount' => 20,
      'readCount' => 153,
      'commentCount' => 4,
      'language' => 
      array (
        'id' => 1,
        'name' => 'English',
      ),
      'user' => 
      array (
        'name' => 'iamsandyhall',
        'avatar' => 'http://a.wattpad.com/useravatar/iamsandyhall.128.340656.jpg',
      ),
      'description' => 'Lea and Gabe are in the same creative writing class. They get the same pop culture references, order the same Chinese food, and hang out in the same places. Unfortunately, Lea is reserved, Gabe has issues, and despite their initial mutual crush, it looks like they are never going to work things out.

But somehow even when nothing is going on, something is happening between them, and everyone can see it. Their creative writing teacher pushes them together. The baristas at the local Starbucks watch their relationship like a TV show. Their bus driver tells his wife about them. The waitress at the diner automatically seats them together. Even the squirrel who lives on the college green believes in their relationship.',
      'cover' => 'http://a.wattpad.com/cover/18265912-256-k534988.jpg',
      'completed' => false,
      'categories' => 
      array (
        0 => 1,
        1 => 4,
      ),
      'tags' => 
      array (
        0 => 'adult',
        1 => 'contemporary',
        2 => 'reads',
        3 => 'swoon',
        4 => 'swoonworthy',
        5 => 'young',
      ),
      'rating' => 3,
      'copyright' => 1,
      'url' => 'http://www.wattpad.com/story/18265912-a-little-something-different',
      'firstPartId' => 56140494,
      'numParts' => 3,
      'parts' => 
      array (
        0 => 
        array (
          'id' => 56140494,
          'title' => 'A Little Something Different (September and October)',
          'draft' => false,
          'modifyDate' => '2014-07-14T19:45:12Z',
          'length' => 723,
          'videoId' => '',
          'photoUrl' => '',
          'commentCount' => 1,
          'voteCount' => 6,
          'readCount' => '91',
        ),
        1 => 
        array (
          'id' => 56140699,
          'title' => 'September (Maribel - Lea\'s roommate)',
          'draft' => false,
          'modifyDate' => '2014-07-14T19:48:02Z',
          'length' => 2611,
          'videoId' => '',
          'photoUrl' => '',
          'commentCount' => 0,
          'voteCount' => 6,
          'readCount' => '37',
        ),
        2 => 
        array (
          'id' => 56140763,
          'title' => 'September (Inga - creative writing professor)',
          'draft' => false,
          'modifyDate' => '2014-07-15T18:21:20Z',
          'length' => 6036,
          'videoId' => '',
          'photoUrl' => '',
          'commentCount' => 3,
          'voteCount' => 8,
          'readCount' => '27',
        ),
      ),
      'deleted' => false,
    ),
  ),
  'total' => 1,
  'categories' => 
  array (
    0 => 
    array (
      'id' => 4,
      'name' => 'Romance',
      'count' => 1,
    ),
    1 => 
    array (
      'id' => 1,
      'name' => 'Teen Fiction',
      'count' => 1,
    ),
  ),
  'tags' => 
  array (
  ),
)

typeof(data) 返回string

哦 - 似乎是一个 php 数组

4

4 回答 4

1

你能在解析之前显示数据的console.log吗?

Angular 的 JSON 格式有时与原生 javascript 的 json 格式不同,您可能想尝试使用 angular.fromJson 而不是 JSON.parse。

于 2014-07-16T05:37:17.053 回答
0
response already parsed so you no need to parse again. try to return data instead parsing ,   

query: {method:'GET',
        params:{query:'margaretatwood'},
        isArray:false,
        transformResponse: function(data, headers){
        return data;
        }
} 

仍然如果你想解析,使用角度 json 转换方法

angular.toJson() angular.fromJson()

于 2014-07-16T05:40:11.823 回答
0

原来我从响应中得到了一个 php 数组。通过设置固定

headers: {
  'Accept': 'application/json'
}

感谢 DavidLin 帮我解决了这个问题!

于 2014-07-16T19:08:41.480 回答
0

资源 0:“f” 1:“a” 2:“l” 3:“s” 4:“e”

This finally worked for me:

transformResponse: function (data, headersGetter) {
return { isCorrect: angular.fromJson(data) }
}
于 2015-05-26T18:57:08.580 回答