Make ajax request with user IP

Asked

Viewed 178 times

2

When I make the request in some site vi ajax, takes the server IP as requesting, I have to put the user IP as the responsible for the request?

  • 1

    It will only take the server IP if you are making the request on the server side

  • Use this API to send the user IP to the server: http://ip-api.com/

1 answer

0

Following example:

$('#enviar').click(function () {
    $.ajax({
        method: "GET",
        url: "http://ip-api.com/json"
    }).success(function (msg) {

        $.ajax({
            method: "POST",
            url: "http://posttestserver.com/post.php",
            data: {
                ip: msg.query
            }
        }).success(function (e) {
            $('#url')[0].innerHTML = e;
            $('#ip')[0].innerHTML = msg.query;
            alert('Salvo com sucesso.');
        }).error(function (e) {
            alert('Ocorreu um erro. Mensagem: ' + e);
        });


    }).error(function (msg) {
        alert('Erro: ' + msg);
    });
});

Click here to open an example in Jsfiddle.

  • Ah understood.. But then in case, I would have to know how the requested page treats the IP? My request is Cross Domain..

  • No need because it is an API

Browser other questions tagged

You are not signed in. Login or sign up in order to post.