2

I got a sample API online. I want to use it for my Angular sample app. But, I'm not able to access the below restful API. Please have a look at my code.

   function Hello($scope, $http) {
    $http({method: 'GET', url: 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'}).
    success(function(data) {
        alert("Success");           
    }).
    error(function(data){
       alert("Error");
    });

}

Thank you 
Nizam
5
  • Does the API endpoint support JSONP? Commented Jan 16, 2014 at 4:37
  • @Jason, I'm not sure about it. I'm a new bie to Angular. If that requires, what should I do? or can you post one example? Commented Jan 16, 2014 at 4:39
  • It looks like it does, add a &callback={some local function name like render} to the end of the URL and then you can use this service across domains. I don't have time to write a full answer but I'm sure someone else is already typing one :) Commented Jan 16, 2014 at 4:40
  • @Jason, Thank you for the answer. But, I'm not able to get you what you said. Commented Jan 16, 2014 at 4:41
  • Incase anyone else needs help writing that answer this page has the documentation for JSONP requests: docs.angularjs.org/api/ng.$http Commented Jan 16, 2014 at 4:42

1 Answer 1

2

You need to use JSONP in order to request data via Ajax across domains. The good news is you can just replace this code:

function Hello($scope, $http) {

    $http.get('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=atf6ya6bbz3v5u5q8um82pev').
    success(function(data) {
        alert("success");
        $scope.greeting = data["content"];
        console.log(data["content"]);
    }).
    error(function(data){
       alert("Error");
    });

}

With this code (demo):

function Hello($scope, $http) {
    $http
      .jsonp('http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=atf6ya6bbz3v5u5q8um82pev&callback=JSON_CALLBACK')
      .success(function(data) {
        alert("success");
        $scope.greeting = data["content"];
        console.log(data["content"]);
      })
      .error(function(data){
        alert("Error");
      });
}

Here is what it took, the $http.jsonp() method requires the string 'callback=JSON_CALLBACK' to appear somewhere in the URL. It will replace this with a generated function name that the .success() will reference. This won't magically work with servers that don't support JSONP but in this case the server URL does.

Sign up to request clarification or add additional context in comments.

4 Comments

I updated the change from/to code are you still having the error? Maybe you can create a JSFiddle of your code and I can update it
Updated, and getting the success alert. I'm interviewing with Google next month and possibly the AngualrJS team so I wanted to make sure I understood this before I committed an answer to SO. Good luck :)
I wouldn't worry about building a Rolodex of gurus, that's the best part of StackOverflow. The instant you need some key question answered there is some other programmer out there who can help. As long as you vote up and select correct answers that is... Hint hint :) good luck and for the sake of your spam filter I'd remove your comment if I were you.
Thank you Jason. Good luck for you too. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.