我正在使用 jQuery 通过访问者的 IP 地址获取其位置。有一项很棒的服务叫做免费地理IP http://freegeoip.appspot.com/。我需要做的就是在 URL 末尾添加“json”或“xml”,然后添加 IP 地址,它将返回所需的数据。
当我在浏览器中手动执行此操作时,它可以工作:我可以下载一个小文档。当我从浏览器发出 $.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