ajax - 调用 angularJS $http.jsonp 时未捕获的语法错误 : Unexpected token <,

标签 ajax angularjs http jsonp

我正在尝试使用 IUCN Red List网络服务 API ( here's an example output )。不幸的是,除了这个 one-off Gist,我找不到任何文档。 .看起来 API 正在构建一个 HTML 文档而不是返回一个数据对象,这是我过去从未经历过的事情。我还注意到示例中没有提到 ?callback=JSON_CALLBACK。在 URL 中,这是我在处理 JSONP 时所期望的。

我在 AngularJS 中构建了一个 http 请求,如下所示:

atRiskApp.controller('IucnController', ['$scope', '$routeParams', '$http', function ($scope, $routeParams, $http) {
  $scope.iucn = $routeParams.iucn; // pulling a number from the URL: ex. 22718591

  $scope.getIUCN = function () {
    var iucnUrl = 'http://api.iucnredlist.org/details/' + $scope.iucn + '/0.js';

    $http.jsonp( url )
      .success( function (response) {
        console.log(response);
      })
      .error( function (response) {
        console.log(response);
      });
  };
}]);

虽然 HTML 文档已成功传递到我的应用程序,但我收到以下错误消息: Uncaught SyntaxError: Unexpected token <

似乎该应用程序期望获取 Javascript,但却获取了一个 HTML 文档,而它显然无法解析该文档。我试过添加 config反对请求 based on the angular docs : $http.jsonp( {url: iucnUrl, responseType: 'text'} )没有任何运气。

我的问题是,我该如何处理返回的 HTML 文档,还是我偏离了轨道?

API 的响应是一个带有 javascript 扩展名的 HTML 文档: API Response

最佳答案

关于page you linked to在您的评论中,我在 API 索引 标题下发现了一些可能有用的信息。

您实际上可以获得所有分类级别的 JSON,包括您的示例 Aneides aeneus .但是,此 JSON 不包含 HTML 版本中的所有数据,因此用处不大。希望这会有所帮助。

API Index (excerpt)

It is also possible to retrieve the row(s) of the index corresponding to an individual species:

http://iucn-redlist-api.heroku.com/index/species/panthera-leo.json

You can use dashes for spaces, as a convenient replacement for the standard URL escape, %20.

The HTML format contains direct links to the species account pages. The CSV and JSON formats include a species_id column which can be used to construct species account URLs as follows:

http://iucn-redlist-api.heroku.com/details/species_id/0

To use the index JSON in Web pages directly, you may need JSONP padding; use the “.js” extension and add a “callback” parameter with the name of the function to use.

http://iucn-redlist-api.heroku.com/index/genus/Dioscorea.js?callback=show

关于ajax - 调用 angularJS $http.jsonp 时未捕获的语法错误 : Unexpected token <,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22461451/

相关文章:

javascript - 如果 session 过期,如何从ajax响应自动重定向网页?

asp.net - 使用 ScriptManager.RegisterClientScriptResource 的真正优势是什么?

javascript - 将 javascript 变量传递到另一个 php 页面

javascript - 如何从本地存储中取出我的日期并在没有时间的情况下设置它们?

java - 从java中的http请求获取文件

http - POST 不是幂等的后果(RESTful API)

http - PIN URL openstreetmap 传入纬度/经度或其他平台

javascript - 表 td colspan 不适用于具有 ID 的 tr

javascript - Angular : Update one-time binded data

javascript - Angular 的 Highchart 漏斗