Friday wrote a classified information of the small dongdong! Only IP addresses in the database, The average visitor is not sure which city the IP address originated from. If there is one more column in the table, there is no truth to it. It would be nice if you could turn the IP address into a city. Of course you can go down to the database. But it's a bit wasteful for this demand. It's okay. There are a lot of websites that provide inquiries. If I can turn it into mine. Problem solving .
A JS function is required. When each record comes out, replace the IP with the city:
Copy 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>
A list of my search URLs:
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
Above two only returns text
Http://www.youdao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123
This returns the XML
Finished, aware of a problem. Ajax does not support Cross-domain submissions. This is not a problem: we use a script to write a page. Load the target URL with MSXML's Load Method! Reference this post: http://topic.csdn.net/t/ 20030619/12/1933920.html
JS code slightly changed:
Copy 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 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&am P;q= "
Set Parser=server.createobject (" MSXML2. DOMDocument ")
Parser.async=false
Parser. Validateonparse=true
Parser.setproperty "ServerHTTPRequest", True
Parser.load (strurl)
If Parser.parseerr Or.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)
%>