我使用jQuery通过他们的IP地址获取访问者的位置。这种称为freeGeoIP的服务很棒。我需要做的只是在URL末尾加上'json'或'xml',然后添加IP地址,它会返回所需的数据。jQuery的JSON请求获得'200 OK'的答案,但没有内容
当我在浏览器中手动执行此操作时,它工作正常:我得到一个小文档以供下载。当我从浏览器执行$ .ajax或$ .getJSON请求时,它会以'200 OK'标题和下面的元数据回答。但没有实际的数据进来。发生了什么?
编辑:我添加的JavaScript/jQuery代码:
function openForm(event,ui){
var _this = $(this);
//Get details on the user's IP
var myIP = $('#yourIP').attr('ip');alert(myIP);
var url = 'http://freegeoip.appspot.com/json/' + myIP;
$.ajax({
url: url,
dataType: 'json',
contentType: 'text/json',
timeout: 10000,
complete: function(ip){
alert('Success Ajax!');
//URL returns status,ip,countrycode,countryname,regioncode,regionname,city,zipcode,latitude,longitude
$('#yourIP').text(ip.city + ", " + ip.countryname + " at " + ip.latitude + " latitude.");
$('#yourIP').attr({'city': ip.city,'country': ip.countryname});
}
});
RESPONSE HEADERS
Cache-Control no-cache
Content-Type text/json
Expires Fri, 01 Jan 1990 00:00:00 GMT
Content-Encoding gzip
Date Fri, 17 Dec 2010 15:26:48 GMT
Server Google Frontend
Content-Length 156
REQUEST HEADERS
Host freegeoip.appspot.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)
Accept application/json, text/javascript, */*; q=0.01
Accept-Language nl,en-us;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type text/json
Referer http://www.freshbase.nl/permaculture/index.php
Origin http://www.freshbase.nl
由于您可能没有正确配置您的$ .ajax()函数,因此您应该提供代码片段。 – Soviut 2010-12-17 15:38:03
为什么你不能从请求头获取remote_addr?我第二个看到代码的概念。 – jMyles 2010-12-17 15:41:02
当然,代码即将出现! – Wytze 2010-12-17 15:47:33