使用带有 JavaScript 的 hostip 进行地理定位
这可能是一个非常愚蠢的问题。我正在尝试使用 http: 提供的 api 进行地理定位,以根据用户的 IP 地址查找用户地址: //www.hostip.info/use.html。我将其与 jquery 结合使用,代码如下:
$.get("http://api.hostip.info/get_html.php", function(data){
alert("Data Loaded: " + data);
});
遗憾的是,这似乎不起作用。警报永远不会被触发,所以我假设呼叫永远不会返回。以前有人这样做过吗?谢谢
This is probably a very silly question. I am trying to do a geolocation to find a users address based on their ip address using the api provided by http://www.hostip.info/use.html. I am using this in conjuction with jquery with the following code:
$.get("http://api.hostip.info/get_html.php", function(data){
alert("Data Loaded: " + data);
});
Sadly, this doesn't seem to work. The alert is never triggered so i assume the call never returns. Has anyone done this before? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为给他们打电话没有成功。跨域限制可能会阻止您读取结果数据。如果 hostip 提供 JSONP API,那么可用,但我在他们的网站上没有看到任何提及。
I don't think the call to them is successful. Cross-domain limitations are likely to prevent you from reading the result data. If hostip offered a JSONP API, that would be usable, but I didn't see any mention of one on their site.
如果您使用 Google 的 AJAX API,那么使用其 客户端位置< 即可轻松获取位置/a> 功能——这不需要任何跨域调用。
否则,正如其他人指出的那样,您将需要提供 JSONP 的服务,或者您需要在自己的服务器上编写代理来为您获取数据。
If you use Google's AJAX API, then it's really easy to get location using their Client Location functionality -- this doesn't require any cross-domain calls.
Otherwise, as others have pointed out, you'll need a service that provides JSONP or you'll need to write a proxy on your own server to get the data for you.
您无法对 XML 数据进行跨域调用。其他站点提供 JSON 接口:
您可以使用 YUI GET Utility 跨域调用该接口或通过 JQuery。
You can't make cross-domain calls for XML data. Other sites offer a JSON interface:
which you can cross-domain call using the YUI GET Utility or via JQuery.
您无法从 javascript 调用外部域。这绝对是问题所在。您要么需要在服务器上设置一个代理脚本来为您获取远程页面,要么找到一个比上面提到的实现 JSONP 的服务。
You can't make calls to foreign domains from javascript. This is definitely the problem. You either need to set up a proxy script on your server that fetches remote pages for you, or find a service than implements JSONP as mentioned above.