A js function is needed. Every time a record comes out, the ip is replaced with the city. Friday wrote a small piece of classified information! There is only the ip address in the database, and the average visitor does not know which city the IP address comes from. If in There is one more column in the table to save the city, but there is no authenticity at all. It would be great if the IP address could be turned into a city. Of course, you can go down and download the database. But it feels a bit wasteful for this requirement. Fortunately, there are many websites that provide query .If I can turn its result into my .problem solved.
A js function is needed. Whenever a record comes out, replace the ip with the city:
Copy the code code as follows:
<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>
Write a test case:
<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>
Let’s list a few query URLs I found:
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
The above two only return text
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123
This returns XML
After finishing writing, I realized a problem. Ajax does not support cross-domain submission. This is not a problem: we use a script to write a page. Use the load method of msxml to load the target URL! Then we can get the data we are interested in! Refer to this post: http://topic.csdn.net/t/20030619/12/1933920.html
Change the js code slightly:
Copy the code code as follows:
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){}
}
The following is the ASP file source code:
Copy the code code as follows:
<%
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)
%>