需要一个js函数.当每出来一条记录时,就把ip替换为城市:
<script type="text/javascript">
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='http://ip.wanvee.cn/GetIp.ashx?ipstr='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
var resContent=strResponse.responseText;
var strStruct=resContent.substring(resContent.lastIndexOf(",")+1,resContent.length);
qIp.innerHTML=strStruct.split(" ")[0];
}
});
}catch(e){}
}
</script>
写一个测试用例:
<span id="ip_2">221.123.123.123</span><script type="text/javascript">queryAddress('2')</script>
<span id="ip_3">221.123.123.123</span><script type="text/javascript">queryAddress('3')</script>
<span id="ip_4">221.123.123.123</span><script type="text/javascript">queryAddress('4')</script>
罗列一下我找的几个查询网址:
http://www.ip.cn/getip.php?action=queryip&ip_url=221.123.123.123
http://ip.wanvee.cn/GetIp.ashx?ipstr=221.123.123.123
以上两个只返回文本
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123
这个返回XML
写完了,意识到一个问题.ajax不支持跨域提交.这也不成问题:我们用脚本写一个页面.用msxml的load方法装载目标网址!就可以搞到我们感兴趣的数据!参考此贴:http://topic.csdn.net/t/20030619/12/1933920.html
js代码稍微变一下:
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='queryiplocal.asp?ip='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
qIp.innerHTML=strResponse.responseText;
}
});
}catch(e){}
}
下面的是ASP文件源码:
<%
Response.ContentType="text/xml"
Response.Charset="GB2312"
Dim strIP,strPattern
strIP=Request.QueryString("ip")
strPattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
If strIP="" Or IsNumeric(strIP) then
Response.End()
ElseIf Not serRegValidate(strPattern,strIP) then
Response.End()
End If
Dim strURL:strURL="http://www.youdao.com/smartresult-xml/search.s?type=ip&q="
set parser=Server.CreateObject("MSXML2.DOMDocument")
parser.async=false
parser.ValidateOnParse=true
parser.setProperty "ServerHTTPRequest",true
parser.load(strURL)
if parser.parseError.errorCode<>0 then
Response.End()
end if
set currNode=parser.selectNodes("//product")
Dim strLocal:strLocal=currNode.item(0).selectSingleNode("location").text
Response.Write Split(strLocal," ")(0)
%>