gpt4 book ai didi

javascript - 如何将页面中以文本形式表示的 IP 地址替换为其 IP 位置(例如 "Country, Region, City")

转载 作者:行者123 更新时间:2023-11-28 21:26:47 24 4
gpt4 key购买 nike

我正在尝试编写 Opera 用户 JS,以用其 IP 位置替换页面中以文本形式表示的 IP 地址。经过谷歌搜索一段时间后,我找到了一项服务,它提供了我所需要的信息。

此 HTTP 查询服务返回页面的 header ,其中包含包含所需信息的元数据。
例如,此查询:http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111
(参见页面来源)
在浏览器中此请求工作正常。
我尝试通过 AJAX 获取此文本(作为 XML),但在 xmlhttp.send(); 行遇到错误 ReferenceError: Security Violation

所以,我的问题是:
- 是否存在通过JS从IP地址获取IP位置的做法(无需任何费用||注册)
- 如何通过JS将一个页面的部分内容替换为另一页面的修改内容?

PS。我对 JS 编程很陌生。

  <html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
var x = xmlhttp.responseXML.documentElement.documentElement;
var i, s, xx;
s = '';
for (i=0; i < x.length; i++)
{
xx = x[i].documentElement;
if (xx.attributes[0] == 'country') s = s + xx.attributes[1].textContent;
if (xx.attributes[0] == 'region') s = s + ', ' + xx.attributes[1].textContent;
if (xx.attributes[0] == 'city') s = s + ', ' + xx.attributes[1].textContent;
}
document.getElementById("myDiv").innerHTML = s;
}
}
xmlhttp.open("GET","http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111",false);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

包含测试页。

好的,非常感谢大家!
可能是我的问题不够清楚。
让我们再试一次(我告诉自己):
我需要像 IPAddress2IPLocation(InIPAddress) 这样的 JS 函数,它使用传递的参数以 Country, Region, City 格式返回 IP 位置。

有人知道我的问题的答案吗?
此时(此处为凌晨 3:50)我批准了任何建议:)
但是,无论如何,这与我的生活无关——这只是 JS 的有趣之处。

最佳答案

Javascript 安全!不用担心,有一些方法可以解决同源政策。

Getting around same origin policy in javascript without server side scripts

关于javascript - 如何将页面中以文本形式表示的 IP 地址替换为其 IP 位置(例如 "Country, Region, City"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4840293/

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