2

i know good javascript but totally novice in json and need to do the following:

get the geolocation from IP , with "ipinfodb" like this response:

OK;;174.88.229.95;CA;CANADA;QUEBEC;MONTREAL;H1A 0A1;45.5088;-73.5878;-04:00

i found many codes for that but all seem to be complicate and long with more options like saving the results in cookies i want the least necessary code to retrieve those informations for saving them in cookies and more i want to care for it my self after.. (i don't like to put code i dont understand) the best would be a simple function that returns this information as string or array, like this

function getLocFromIP(IP){
    (js + json code)
    return result;
}

much thank in advance


thank you all for responding i was trying to filter out the solution, and it works but i'm not yet well satisfied of the result.. i gonna reformulate my question here: is there i can retrive the geo location from ip with simplest way (javascript) that means without json or jquery.. ajax would be perfect but it doesnt work inter domain (different domains) thanks again

OK;;174.88.230.88;CA;CANADA;QUEBEC;MONTREAL;H1A 0A1;45.5088;-73.5878;-04:00
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Malek
  • 21
  • 2

3 Answers3

0

The example you posted isn't valid JSON (you can check if something is valid JSON somewhere like here). That said, looking at the ipinfodb website they appear to have a way to get JSON output using their API (see here).

Once you have valid JSON, you can use JSON.parse. See here for some examples on using it.

So the general idea is

function getLocFromIP(IP){
    //(js code to use ipinfodb API to get valid JSON reponse)
    //(js code using JSON.parse to parse the received JSON reponse)
    //(js code to get whatever you want as the result)
    return result;
}

As a side note, if you use jQuery you can also use jQuery.parseJSON to parse JSON (see here) which will be more reliable if you anticipate clients using older browsers that might not support JSON.parse.

Christian Wilkie
  • 3,693
  • 5
  • 34
  • 49
0

IP location XML API requires some query strings like &format=json&callback=? additional to ip address in order to get json.

Then you get json like this:

{
    "statusCode": "OK",
    "statusMessage": "",
    "ipAddress": "175.108.204.0",
    "countryCode": "JP",
    "countryName": "JAPAN",
    "regionName": "TOKYO",
    "cityName": "TOKYO",
    "zipCode": "214-002",
    "latitude": "35.6149",
    "longitude": "139.581",
    "timeZone": "+09:00"
}

Here is a sample code (thanks to @iveoak for good template):

function getLocFromIP(IP){
    //(js code to use ipinfodb API to get valid JSON response)
    var url = 'http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=' + ip + '&format=json&callback=?';
    var response = $.getJSON(url); // using jQuery

    //(js code using JSON.parse to parse the received JSON response)
    response.then(function (location) {
        var info = '<ul>';
        for (var key in location) {
            info += '<li>' + key + ': ' + location[key] + '</li>';
        }
        info += '</ul>';
    });

    //(js code to get whatever you want as the result)
    return result;
}

If you do not use jQuery, see How can I open a JSON file in JavaScript without jQuery?.

Sample (using jQuery): http://jsfiddle.net/tokkonoPapa/tevB4/

Community
  • 1
  • 1
tokkonopapa
  • 167
  • 4
0

I post another answer.

This is a pure javascript solution using jsonp. Basically, it's a same concept using <script src="http://api.ipinfodb.com/..."></script>.

So you can accsess from anywhere, and do not need to handle json directly. You can simply get the javascript object.

Here is my demo: http://jsfiddle.net/tokkonoPapa/vCTpK/

UPDATE:

I/F is slightly different what you want. It uses a callback function.

getLocFromIP(ip, function(result) {
    // use result
});

and the demo returns:

OK;;175.108.204.0;JP;JAPAN;TOKYO;TOKYO;214-002;35.6149;139.581;+09:00

I hope this basic solution satisfies you.

tokkonopapa
  • 167
  • 4