Thillai Narayanan
Thillai Narayanan

Reputation: 4896

Not able to fetch the json response through angularjs

Need to fetch the build values from apache.org. So i am using their api https://builds.apache.org/api/json

I tried angularjs $http.jsonp but not able to fetch the data. In chrome console under network json api is getting loaded but the data is not getting returned instead it is throwing the response as error.

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')
    });
 });

Getting the error as

Uncaught SyntaxError: Unexpected token : 
error

Upvotes: 1

Views: 1180

Answers (1)

maddyblue
maddyblue

Reputation: 16882

As per the jsonp angular docs, you must append JSON_CALLBACK to the URL: https://builds.apache.org/api/json?jsonp=JSON_CALLBACK

However, that URL doesn't work because even when the callback parameter is specified, the server still sends back a content-type of application/json, instead of the expected application/javascript. This causes it to be parsed (evidently) by the json parser instead of the javascript callback needed for JSONP to work. I'm not versed enough in JSONP or Angular to know who is it fault here.

I've made a fiddle with this working with another URL.

[Update]: The apache build server appears to use Jenkins, which has disable JSONP from the remote API. You can verify this yourself by trying to hit their jsonp endpoint, which returns a 403. You'll have to use another endpoint, no way I can see around this.

Upvotes: 3

Related Questions