0

I use a free api in order to gather informationfrom the users Ip-adress.

This is the method:

public JsonResult GetIp()
        {

            string url = "http://ip-api.com/json";

            dynamic googleResults = new Uri(url).GetDynamicJsonObject();

            return Json(googleResults, JsonRequestBehavior.AllowGet);
        }

The method is supposed to get called when the page is rendered so I am thinking something like this:

$(document).ready(function () {

        $.ajax({
            type: "POST",
            url: '@Url.Action("GetIp", "Home")',
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            //Here i need code that receives the Json from the controller...
        });
    });

Also, is this the best way to go about a task like this? I've been reading that Json could also be passed as a simple string? Any tips on best practice appreciated. Thanks.

Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Soren Jul 24 '14 at 21:22

2 Answers2

2
$(document).ready(function () {
    $(window).load(function(){

        $.ajax({
            type: "POST",
            url: '@Url.Action("GetIp", "Home")',
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function(resp){
                //resp is the returned json variable from the server side
                //you can use it like "resp.id" or "resp.anyOtherValue"
            }
        });
    });
    });
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
  • Thank you very much for answering. However, this should mean that if I put: Alert(resp.Country) in the response. It would Alert it? I get an Alert: undefined. When I put a breakpoint at the return-value of the method it shows that I got the document there. – user2915962 Jul 24 '14 at 21:28
  • are you sure about the `.Country`? is it not miss-spelled or something? because it must be alerted if everything's fine on the server side! – Amin Jafari Jul 24 '14 at 21:32
  • Yes, i am sure, if you paste: http://ip-api.com/json In your browser you will get a Json containing for examle .country. Thanks – user2915962 Jul 24 '14 at 21:33
  • ok so it's `.country` with small "c", remember that variables are case sensitive! you must use it as `resp.country` – Amin Jafari Jul 24 '14 at 21:35
1

You won't be getting the client ip this way, you'll get the server ip, because that's the machine making the call.

What you want is to make that call directly, in javascript, so the request ip is the client's.

If you look at the response headers from that api, Access-Control-Allow-Origin = *, so you should be able to use that url directly.

And as Amin mentioned, just add a success method to handle the response, like this:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: 'http://ip-api.com/json',
        dataType: "json",
        success: function(resp){
            //resp.query
        }
    });
});

Working jsfiddle

Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
  • Thank you! Not sure if I understand...If I paste: http://ip-api.com/json In my browser, I get all the info i need. I imagine that if you used my application, the Json would contain values according to your position? – user2915962 Jul 24 '14 at 21:34
  • Exactly! Each user will get it's own data. – Jaime Gómez Jul 24 '14 at 21:35
  • Aha..so i dont even need the method? The problem seems to be that the method does not return anything back to the Ajax. – user2915962 Jul 24 '14 at 21:38