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:
- <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:
- 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:
- <%
- 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)
- %>