0

I'd like to grab a client IP address using javascript and send it over to my server using AJAX request. Any suggestions on how I can achieve this would be awesome. Some code example would be even better. Thanks in advance!

John D.
  • 111
  • 2
  • 14
  • What have you tried and why not use the server to get the clients IP rather than sending it to something that can already get it? – NewToJS Nov 04 '16 at 19:50

2 Answers2

3

Front-end Javascript by itself doesn't have access to anything that will tell it a user's IP address. See here:

How to get client's IP address using JavaScript?

However, like it says in that answer, there are APIs you can use to get the IP address.

More importantly, though, any server receiving your AJAX request will have automatic access to the IP address. So if you can set up code on the server you should be good to go, or at least will have a different question.

If you don't have control over the server, and instead are doing something like POSTing data to a webforms API, then using an API is your best bet.

RobertAKARobin
  • 3,933
  • 3
  • 24
  • 46
0

Client-side JavaScript has no access to the network addresses. What you're specifically asking for isn't possible.

You can determine the closest routable address for the client server-side, but this could always be an upstream proxy or NAT router.

Brad
  • 159,648
  • 54
  • 349
  • 530