AJAX (XMLHttpRequest) cross-domain request method in detail (iii)

Source: Internet
Author: User
Tags dotnet

Meaning: The following code is tested in Firefox 3.5, Chrome 3.0, and Safari 4 versions. The implementation of IE8 is different from other browsing methods.

3, request with authentication information

Authentication is a common problem in web development, and in cross-domain requests, no authentication information is sent by default. To send the verification information, you need to do the Withcredentials attribute, here is an example of a simple request:

[XHTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
  2. "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title> dimant Ajax cross-domain request test </title>
  6. </head>
  7. <body>
  8. <input type=' button ' value=' Start testing ' onclick=' crossdomainrequest () ' />
  9. <div id="Content"></div>
  10. <mce:script type="Text/javascript"><!--
  11. var xhr = new XMLHttpRequest ();
  12. var url = ' http://dotnet.aspx.cc/RequestsWithCredentials.aspx ';
  13. function Crossdomainrequest () {
  14. document.getElementById ("Content"). InnerHTML = "Start request ...";
  15. if (XHR) {
  16. Xhr.open (' GET ', url, true);
  17. Xhr.onreadystatechange = handler;
  18. xhr.withcredentials = "true";
  19. Xhr.send ();
  20. } else {
  21. document.getElementById ("Content"). InnerHTML = "Cannot create XMLHttpRequest.  ";
  22. }
  23. }
  24. function Handler (EVTXHR) {
  25. if (xhr.readystate = = 4) {
  26. if (xhr.status = =) {
  27. var response = Xhr.responsetext;
  28. document.getElementById ("Content"). InnerHTML = "Result:" + response;
  29. } else {
  30. document.getElementById ("Content"). InnerHTML + = "<br/> Execution Status:" + Xhr.status;
  31. }
  32. }
  33. else {
  34. document.getElementById ("Content"). InnerHTML + = "<br/> Execution Status readyState:" + xhr.readystate;
  35. }
  36. }
  37. --></mce:script>
  38. </body>
  39. </html>

Click "Start Test", we can detect the following request execution process:

[XHTML]View Plaincopy
  1. Get/requestswithcredentials.aspx http/1.1
  2. Host:dotnet.aspx.cc
  3. user-agent:mozilla/5.0 (Windows; U Windows NT 5.2; ZH-CN; rv:1.9.1.7) gecko/20091221 firefox/3.5.7 (. NET CLR 3.5.30729)
  4. Accept:text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8
  5. Accept-language:zh-cn,zh; q=0.5
  6. Accept-encoding:gzip,deflate
  7. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  8. keep-alive:300
  9. Connection:keep-alive
  10. Referer:http://www.meng_xian_hui.com:801/crossdomainajax/requestswithcredentials.html
  11. origin:http://www.meng_xian_hui.com:801
  12. http/1.x OK
  13. Date:sun, 14:12:26 GMT
  14. server:microsoft-iis/6.0
  15. X-powered-by:asp.net
  16. x-aspnet-version:2.0.50727
  17. access-control-allow-origin:http://www.meng_xian_hui.com:801
  18. Access-control-allow-credentials:true
  19. Set-cookie: asp.net_sessionid=fn2zf0zq1cuwgf45fm5fw145; path=/; HttpOnly
  20. Set-cookie: visit=1; expires=Sun, 10-jan-2010 14:12:56 GMT; path=/
  21. Cache-control:no-cache
  22. Pragma:no-cache
  23. Expires:-1
  24. content-type:text/html; charset=utf-8
  25. Content-length:1

As you can see from the response above, cookies are sent along with the request. If we click the Test button multiple times, we can see that the result of the request and response is this:

[XHTML]View Plaincopy
  1. Get/requestswithcredentials.aspx http/1.1
  2. Host:dotnet.aspx.cc
  3. user-agent:mozilla/5.0 (Windows; U Windows NT 5.2; ZH-CN; rv:1.9.1.7) gecko/20091221 firefox/3.5.7 (. NET CLR 3.5.30729)
  4. Accept:text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8
  5. Accept-language:zh-cn,zh; q=0.5
  6. Accept-encoding:gzip,deflate
  7. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  8. keep-alive:300
  9. Connection:keep-alive
  10. Referer:http://www.meng_xian_hui.com:801/crossdomainajax/requestswithcredentials.html
  11. origin:http://www.meng_xian_hui.com:801
  12. Cookie: asp.net_sessionid=fn2zf0zq1cuwgf45fm5fw145; visit=2
  13. http/1.x OK
  14. Date:sun, 14:13:58 GMT
  15. server:microsoft-iis/6.0
  16. X-powered-by:asp.net
  17. x-aspnet-version:2.0.50727
  18. access-control-allow-origin:http://www.meng_xian_hui.com:801
  19. Access-control-allow-credentials:true
  20. Set-cookie: visit=3; expires=Sun, 10-jan-2010 14:14:28 GMT; path=/
  21. Cache-control:no-cache
  22. Pragma:no-cache
  23. Expires:-1
  24. content-type:text/html; charset=utf-8
  25. Content-length:1

Pay attention to cookie:asp.net_sessionid=fn2zf0zq1cuwgf45fm5fw145; visit=2 this line, the access counter has been sent together to the server.

The implementation method in 4,IE8

IE8 has started to support cross-domain access to resources, but the functionality provided by IE8 is simple enough to make a simple request, and here's an example of how to use it:

[XHTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
  2. "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <HTML xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title> dimant Ajax cross-domain request test </title>
  6. </head>
  7. <body>
  8. <input type=' button ' value=' Start testing ' onclick=' crossdomainrequest () ' />
  9. <div id="Content"></div>
  10. <mce:script type="Text/javascript"><!--
  11. var xhr = new Xdomainrequest ();
  12. var url = ' http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx ';
  13. function Crossdomainrequest () {
  14. document.getElementById ("Content"). InnerHTML = "Start ...";
  15. if (XHR) {
  16. Xhr.open (' GET ', url);
  17. xhr.onload = handler;
  18. Xhr.send ();
  19. } else {
  20. document.getElementById ("Content"). InnerHTML = "Cannot create xdomainrequest";
  21. }
  22. }
  23. function Handler (EVTXHR) {
  24. document.getElementById ("Content"). InnerHTML = "Result:" + xhr.responsetext;
  25. }
  26. --></mce:script>
  27. </body>
  28. </html>

In addition, IE8 is implemented differently than other browsers. For more information, please refer to the Xdomainrequest object with the address:
http://msdn.microsoft.com/zh-cn/library/cc288060 (vs.85). aspx

Finally, a friend willing to test can visit this http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx address for a "simple request" test, this page allows any address for cross-domain access

AJAX (XMLHttpRequest) cross-domain request method in detail (iii)

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.