简体   繁体   中英

Trouble getting json from API with angular

Trying to pull in some data using angular and an API. Obviously I'm quite new to this.

My custom service:

readIp.service('ip', ['$resource', function($resource){
    this.getIP = function(ip) {
        var ipData = $resource("http://www.telize.com/jsonip?callback=getip", {
            callback : "JSON_CALLBACK" 
        }, { 
            get : { 
                method: "JSONP" 
            }
        });
        return ipData.get({ getip: ip });
    }
}]);

From my controller:

$scope.getIP = ip.getIP($scope.getip);

HTML:

<strong>Your IP is:</strong>&nbsp;{{ getIP.ip }}

I'm getting an error currently:

Uncaught ReferenceError: getip is not defined 

as the API shows up as: getip({"ip":"###.###.##.##"}); from the source.

Your service isn't properly defined. It should return an object that contains your getIp method. try something along the lines of :

readIp.factory('ip', ['$resource', function($resource){
  return {
    getIP: function(ip) {
      // your code goes here
    }
  }
}]);

try this in place of above code. Hope you have already added ngResource module.

readIp.factory('ip',['$resource',function($resource){ return $resource('http://www.telize.com/jsonip?callback=getip', {}, { query: {method:'GET', params:{}} }); }])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM