The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
$.getJSON to get visitor IP
Discuss $.getJSON to get visitor IP in the JavaScript Development forum on Dev Shed. $.getJSON to get visitor IP JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 16th, 2012, 11:06 AM
|
 |
Contributing User
|
|
|
|
$.getJSON to get visitor IP
This outputs nothing.....Am I retarded?
PHP Code:
<script type="text/javascript">$.getJSON('http://www.option9.com/ip.php', function(data){document.write('Current IP Address: ' + data.ip);});</script>
PHP Code:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
print json_encode(array('ip' => $ip));
?>
the ip.php page outputs: {"ip":"**.191.246.5"}
|

August 16th, 2012, 12:38 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
|
Perhaps the problem is the use of document.write(). Try using a different method for seeing the results of that request.
|

August 16th, 2012, 04:43 PM
|
 |
Lost in code
|
|
|
|
|
Are you running this JavaScript code on www.option9.com?
|

August 16th, 2012, 06:54 PM
|
 |
Contributing User
|
|
|
|
|
I have tried several variations of functions and syntax and have tried to alert the output with still no success. Yes I am running it on option9.com
|

August 16th, 2012, 07:42 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
What do you get when you run this?
Code:
$.getJSON('http://www.option9.com/ip.php', function(data,status,xhr){
alert('Current IP Address: ' + data.ip+'\n'+status+'\n'+xhr.status);
});
http://api.jquery.com/jQuery.getJSON/
|

August 16th, 2012, 08:58 PM
|
 |
Contributing User
|
|
|
|
|
I hate this crap. It doesn't do anything.
|

August 16th, 2012, 09:12 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Have you checked your browser's error console?
P.S. Some people who frequent forums like this are available as consultants. 
|

August 16th, 2012, 10:20 PM
|
 |
Contributing User
|
|
|
|
|
Yes, no errors or warnings come up relevant to the j code.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|