【问题标题】:Not able to fetch the json response through angularjs无法通过 angularjs 获取 json 响应
【发布时间】:2013-10-10 00:36:04
【问题描述】:

需要从 apache.org 获取构建值。所以我正在使用他们的 api https://builds.apache.org/api/json

我尝试了 angularjs $http.jsonp 但无法获取数据。 在网络 json api 下的 chrome 控制台中,正在加载但没有返回数据,而是将响应作为错误抛出。

app.controller("jsoncontroller",function($scope,$http){
   var url='https://builds.apache.org/api/json';
  $http.jsonp(url).success(function(data){
       console.log('success');
    })
    .error(function () {
      console.log('error')
    });
 });

得到错误

Uncaught SyntaxError: Unexpected token : 
error

【问题讨论】:

    标签: json angularjs


    【解决方案1】:

    根据jsonp angular docs,您必须将JSON_CALLBACK 附加到URL:https://builds.apache.org/api/json?jsonp=JSON_CALLBACK

    但是,该 URL 不起作用,因为即使指定了回调参数,服务器仍会发回 application/json 的内容类型,而不是预期的 application/javascript。这导致它(显然)由 json 解析器解析,而不是 JSONP 工作所需的 javascript 回调。我对 JSONP 或 Angular 不够精通,不知道这是谁的错。

    我已经创建了一个fiddle,它与另一个 URL 一起使用。

    [更新]:apache 构建服务器似乎使用 Jenkins,它具有来自远程 API 的disable JSONP。您可以通过尝试点击他们的jsonp endpoint 来验证这一点,这会返回 403。您将不得不使用另一个端点,我无法看到这一点。

    【讨论】:

    • 是的同意,但我需要解决错误并需要使用 //builds.apache.org/api/json 来实现
    • 您找到了任何解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多