0

Trying to make an API call when loading one of my views.

controllers.controller("detailsCtrl", ["$scope", "$routeParams", "$filter", "$http", function($scope, $routeParams, $filter, $http) {

  $scope.getCurrent = function(url, id, callBack, apiCall, promise) {
    var url = "api.openweathermap.org/data/2.5/weather?id=2172797";
    var id = "&appid=d436c04d23a5a44329eb8255190a84be";
    var callBack = "&callback=JSON_CALLBACK";
    var apiCall = url += id += callBack;
    var promise = $http.jsonp(apiCall);
    promise.success(function(response) {
      $scope.current = response.current;
      console.log($scope.current);
    });
  };

  $scope.cityName = $filter("filter")($scope.data.list, {
    id: $routeParams.cityId
  })[0];

}]);

using ng-init in the html

      <div ng-controller="detailsCtrl" ng-init="getCurrent()">
    <h1>{{ cityName.name }}</h1>
      <tr>
        <td>lat: {{cityName.coord.lat}}, lon: {{cityName.coord.lon}}</td>
        <td>{{}}</td>
        <td>{{}}</td>
        <td>{{}}</td>
        <td>{{}}</td>
        <td>Clouds: {{cityName.clouds.all}}</td>
        <td>{{}}</td>
        <td>{{}}</td>
      </tr>

  </div>

Keep getting: http://localhost:8080/api.openweathermap.org/data/2.5/weather?id=2172797&appid=d436c04d23a5a44329eb8255190a84be&callback=angular.callbacks._1 , any ideas why?

note Also making another API call in my mainview before the user navigates to the detail view. Tried ng-click when the user switches view, but it didnt make the call at all.

9
  • are you ever tried making the call at the resolve parameter on the route? Commented Oct 18, 2016 at 14:20
  • inspect the request is it fires to proper url? Commented Oct 18, 2016 at 14:20
  • tested the url in the browser, and it works. Commented Oct 18, 2016 at 14:24
  • edit: is that api support jsonp ? Commented Oct 18, 2016 at 14:27
  • can you add html code. I cannot unterstand what you want to do exactly? Commented Oct 18, 2016 at 14:30

2 Answers 2

2

I am using angular2 than angularjs, but believe your problem is more related to not a proper http request. I have just worked out your sample in a plunkr: http://plnkr.co/edit/rOZOyAcAuVcTr3njqKfH?p=preview

getData: function () {
          var obj = {};
          var jsonData = JSON.stringify({ obj: obj }); //For POST
          var req = {
              method: 'POST',
              url: 'http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=d436c04d23a5a44329eb8255190a84be',
          };
          return this.http.post(req.url, req.data, req)
          .toPromise()
          .then(function (response) {
              return response;

          });
      }

There could be one obvious reason that your url is relative to your solution.

Hope this will help.

Thanks

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

Comments

2

Please change the url as below and

var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797";

as your not able to find the url. In your call it will make the url to get appended from the current URL.

3 Comments

right, forgott the http, now i'm getting undefined in the console, only the first API call is logged in the console.
Yes marked it, was the http:// and (response) in the parameter should have been set to current.
That was not your question!

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.