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

编程入门 行业动态 更新时间:2024-10-24 20:23:51
本文介绍了jQuery JSON请求获得'200 OK'的答案,但没有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用jQuery通过IP地址获取访问者的位置。这种称为的服务非常棒。我所需要做的就是在URL末尾加上'json'或'xml',然后添加IP地址,它将返回所需的数据。

I am using jQuery to get visitor's location via their IP address. There's a great service for this called freeGeoIP. All I need to do is tack 'json' or 'xml' at the end of their URL, then add the IP address, and it will return the required data.

当我在浏览器中手动执行此操作时,它可以正常工作:我下载了一个小文档。当我从浏览器执行$ .ajax或$ .getJSON请求时,它会以'200 OK'标题和下面的元数据回答。但没有实际的数据进来。发生了什么?

When I do this manually in my browser, it works: I get a tiny document to download. When I do a $.ajax or $.getJSON request from the browser, it answers with a '200 OK' header and the metadata below. But no actual data comes in. What's going on?

编辑:我已经添加了javascript / jQuery代码:

I've added the javascript/jQuery code:

function openForm(event,ui){ var _this = $(this); //Get details on the user's IP var myIP = $('#yourIP').attr('ip');alert(myIP); var url = 'freegeoip.appspot/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 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 www.freshbase.nl/permaculture/index.php Origin www.freshbase.nl

推荐答案

这是因为您与jQuery AJAX功能中使用的XMLHttpRequest对象的跨域请求限制发生冲突。一些浏览器在发生这种情况时会抛出一个空的200响应(这很令人困惑)。

That's because you're running afoul of the cross-domain request limitation of the XMLHttpRequest object used in jQuery's AJAX features. Some browsers throw an empty 200 response when that happens (which is confusing).

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

You need to either use a service that supports JSONP, to circumvent the cross-domain issue, or use a server-side proxy on the same domain to act as a local intermediary.

更多推荐

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

本文发布于:2023-10-30 22:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:答案   内容   JSON   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!