gpt4 book ai didi

jQuery JSON 请求得到 '200 OK' 答案,但没有内容

转载 作者:行者123 更新时间:2023-11-30 23:49:11 24 4
gpt4 key购买 nike

我正在使用 jQuery 通过访问者的 IP 地址获取其位置。有一个很棒的服务叫做 freeGeoIP 。我需要做的就是在 URL 末尾添加“json”或“xml”,然后添加 IP 地址,它将返回所需的数据。

当我在浏览器中手动执行此操作时,它可以工作:我可以下载一个小文档。当我从浏览器发出 $.ajax 或 $.getJSON 请求时,它会使用“200 OK” header 和下面的元数据进行响应。但没有实际数据进来。这是怎么回事?

编辑:我添加了 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

最佳答案

这是因为您遇到了 jQuery AJAX 功能中使用的 XMLHttpRequest 对象的跨域请求限制。发生这种情况时,某些浏览器会抛出空的 200 响应(这令人困惑)。

您需要使用支持 JSONP 的服务来规避跨域问题,或者使用同一域上的服务器端代理来充当本地中介。

关于jQuery JSON 请求得到 '200 OK' 答案,但没有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4472051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com