Take it for granted: Ajax cross-Origin data submission

Source: Internet
Author: User

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:

  1. <SCRIPT type = "text/JavaScript">
  2. Function queryaddress (Strid ){
  3. Try {
  4. VaR QIP = Document. getelementbyid ("IP _" + Strid );
  5. VaR qurl = 'HTTP: // ip.wanvee.cn/getip.ashx? Ipstr = '+ QIP. firstchild. nodevalue;
  6. VaR Ajax = new Ajax. Request (qurl ,{
  7. Method: 'get ',
  8. Onsuccess: function (strresponse ){
  9. VaR rescontent = strresponse. responsetext;
  10. VaR strstruct = rescontent. substring (rescontent. lastindexof (",") + 1, rescontent. Length );
  11. QIP. innerhtml = strstruct. Split ("") [0];
  12. }
  13. });
  14. } Catch (e ){}
  15. }
  16. </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:

  1. Function queryaddress (Strid ){
  2. Try {
  3. VaR QIP = Document. getelementbyid ("IP _" + Strid );
  4. VaR qurl = 'queryiplocal. asp? IP = '+ QIP. firstchild. nodevalue;
  5. VaR Ajax = new Ajax. Request (qurl ,{
  6. Method: 'get ',
  7. Onsuccess: function (strresponse ){
  8. QIP. innerhtml = strresponse. responsetext;
  9. }
  10. });
  11. } Catch (e ){}
  12. }

 

The following is the source code of the ASP file:

  1. <%
  2. Response. contenttype = "text/XML"
  3. Response. charset = "gb2312"
  4. Dim strip, strpattern
  5. Strip = request. querystring ("ip ")
  6. Strpattern = "^ [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3 }. [0-9] {1, 3} $"
  7. If strip = "" Or isnumeric (strip) then
  8. Response. End ()
  9. Elseif not serregvalidate (strpattern, strip) then
  10. Response. End ()
  11. End if
  12. Dim strurl: strurl = "http://www.youdao.com/smartresult-xml/search.s? Type = IP & Q ="
  13. Set parser = server. Createobject ("msxml2.domdocument ")
  14. Parser. async = false
  15. Parser. validateonparse = true
  16. Parser. setproperty "serverhttprequest", true
  17. Parser. Load (strurl)
  18. If parser. parseerror. errorcode <> 0 then
  19. Response. End ()
  20. End if
  21. Set currnode = parser. selectnodes ("// product ")
  22. Dim strlocal: strlocal = currnode. Item (0). selectsinglenode ("location"). Text
  23. Response. Write split (strlocal, "") (0)
  24. %>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.