IP Location : Angular + Ajax
2 min readOct 4, 2019
Live examples on Repl.it / Codepen / JSFiddle
We’ve added live examples to find IP address location in Angular.
- The example uses $http — a core AngularJS service that relies on XMLHttpRequest or JSONP
- It returns a promise with a response object, that is resolved on request success or rejected on failure.
- Fork the IP Location code samples on Repl.it, JSFiddle or Codepen and edit away !
Angular Module
var app = angular.module('ipLocation', [])
.controller('ipapiCtrl', function($scope, $http){
$scope.getJSON = function() {
// "Search" input provides the IP address
// If it is blank, the client's IP is used
var search = $("#search").val();
// Send an XHR to /json endpoint
$http.get('https://ipapi.co/'+ search + '/json/')
// on success, populate the response in HTML
.then(function(response){
$scope.city = response.data.city;
$scope.region = response.data.region;
$scope.country = response.data.country_name
if (response.data.in_eu){
$scope.eu = 'In Europe';
} else {
$scope.eu = 'Not in Europe';
}
$scope.calling_code = response.data.country_calling_code;
$scope.currency = response.data.currency;
});
}
})
- Working example on repl.it : Fork / View Full page
- Codepen : Coming soon
HTML / CSS / JS code on Github