I wrote a category item on Friday! There is only an IP address in the database. Generally, visitors do not know which city the IP address is from. if there is one more column in the table to store the city, there is no authenticity. how nice is it to change the IP address to a city. of course, you can go down the database. this is a waste of resources. fortunately, many websites provide queries. if you can turn the result into mine. solve the problem.
A js function is required. When each record is generated, the ip address is replaced with the city:
Copy codeThe Code is 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>
List several query URLs I have 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
Only the text is returned.
Http://www.youdao.com/smartresult-xml/search.s? Type = ip & q = 221.123.123.123
The returned XML
After writing, I realized a problem. ajax does not support cross-origin submission. This is not a problem: we use scripts to write a page. We use the msxml load method to load the target URL! We can get the data we are interested in! Refer to this post: http://topic.csdn.net/t/20030619/12/1933920.html
The js Code is slightly changed:Copy codeThe Code is 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 source code of the ASP file:Copy codeThe Code is 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)
%>