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

Source: Internet
Author: User
Tags dotnet

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

2, pre-test request

The preflight request first needs to send an HTTP OPTIONS request header to the resource of another domain name in order to determine whether the actual sent request is secure. The following 2 scenarios require a preflight:
A, not a simple request above, such as a POST request using Content-type for Application/xml or Text/xml
b, set the custom header in the request, such as X-json, X-mengxianhui, etc.

Note: In IIS, you must configure the action of the. aspx extension in the application extensions to allow OPTIONS.

Below we give a preflight 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/PreflightedRequests.aspx ';
  13. function Crossdomainrequest () {
  14. document.getElementById ("Content"). InnerHTML = "Start request ...";
  15. if (XHR) {
  16. var xml = "<root> test </root>";
  17. Xhr.open (' POST ', url, true);
  18. Xhr.setrequestheader ("Powered-by-mengxianhui", "Approve");
  19. Xhr.setrequestheader ("Content-type", "Application/xml");
  20. Xhr.onreadystatechange = handler;
  21. Xhr.send (XML);
  22. } else {
  23. document.getElementById ("Content"). InnerHTML = "Cannot create XMLHttpRequest.  ";
  24. }
  25. }
  26. function Handler (EVTXHR) {
  27. if (xhr.readystate = = 4) {
  28. if (xhr.status = =) {
  29. var response = Xhr.responsetext;
  30. document.getElementById ("Content"). InnerHTML = "Result:" + response;
  31. } else {
  32. document.getElementById ("Content"). InnerHTML = "cannot be accessed across.  ";
  33. }
  34. }
  35. else {
  36. document.getElementById ("Content"). InnerHTML + = "<br/> Execution Status readyState:" + xhr.readystate;
  37. }
  38. }
  39. --></mce:script>
  40. </body>
  41. </html>

In the example above we send data in XML format, and send a nonstandard HTTP header Powered-by-mengxianhui to explain how the server side should set the response header.

On the server side, the contents of preflightedrequests.aspx are as follows:

[XHTML]View Plaincopy
  1. <%@ page language="C #"%>
  2. <mce:script runat="Server"><!--
  3. protected void Page_Load (object sender, EventArgs e)
  4. {
  5. if (Request.HttpMethod.Equals ("GET"))
  6. {
  7. Response.Write ("This page is used to test cross-domain POST requests, direct browsing is of little significance.") ");
  8. }
  9. else if (Request.HttpMethod.Equals ("Options"))
  10. {
  11. Notifies the client that a preflight request is allowed. and set the cache time
  12. Response.clearcontent ();
  13. Response.AddHeader ("Access-control-allow-origin", "http://www.meng_xian_hui.com:801");
  14. Response.AddHeader ("Access-control-allow-methods", "POST, GET, OPTIONS");
  15. Response.AddHeader ("Access-control-allow-headers", "Powered-by-mengxianhui");
  16. Response.AddHeader ("Access-control-max-age", "30");
  17. This procedure does not need to return data
  18. Response.End ();
  19. }
  20. else if (Request.HttpMethod.Equals ("POST"))
  21. {
  22. if (request.headers["Origin"]. Equals ("http://www.meng_xian_hui.com:801"))
  23. {
  24. System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();
  25. Doc. Load (Request.inputstream);
  26. Response.AddHeader ("Access-control-allow-origin", "http://www.meng_xian_hui.com:801");
  27. Response.Write ("The data you submitted is:<br/><br/>" + Server.HTMLEncode (DOC).  OuterXml));
  28. }
  29. Else
  30. {
  31. Response.Write ("Do not allow your site to be requested. ");
  32. }
  33. }
  34. }
  35. --></mce:script>

Click the "Start Test" button and the following series of requests will be executed.

[XHTML]View Plaincopy
  1. Options/preflightedrequests.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. origin:http://www.meng_xian_hui.com:801
  11. Access-control-request-method:post
  12. Access-control-request-headers:powered-by-mengxianhui
  13. http/1.x OK
  14. Date:sun, 14:00:34 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-methods:post, GET, OPTIONS
  20. Access-control-allow-headers:powered-by-mengxianhui
  21. Access-control-max-age:30
  22. Set-cookie: asp.net_sessionid=5npqri55dl1k1zvij1tlw3re; path=/; HttpOnly
  23. Cache-control:private
  24. content-length:0
  25. Post/preflightedrequests.aspx http/1.1
  26. Host:dotnet.aspx.cc
  27. 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)
  28. Accept:text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8
  29. Accept-language:zh-cn,zh; q=0.5
  30. Accept-encoding:gzip,deflate
  31. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  32. keep-alive:300
  33. Connection:keep-alive
  34. Powered-by-mengxianhui:approve
  35. Content-type:application/xml; charset=UTF-8
  36. Referer:http://www.meng_xian_hui.com:801/crossdomainajax/preflightedrequests.html
  37. Content-length:19
  38. origin:http://www.meng_xian_hui.com:801
  39. Pragma:no-cache
  40. Cache-control:no-cache
  41. <root> Test </root>
  42. http/1.x OK
  43. Date:sun, 14:00:34 GMT
  44. server:microsoft-iis/6.0
  45. X-powered-by:asp.net
  46. x-aspnet-version:2.0.50727
  47. access-control-allow-origin:http://www.meng_xian_hui.com:801
  48. Set-cookie: asp.net_sessionid=byvose45zmtbqy45d2a1jf2i; path=/; HttpOnly
  49. Cache-control:private
  50. content-type:text/html; charset=utf-8
  51. Content-length:65

The above code reflects the execution of the preflight request: First, the OPTIONS request header is sent to the server to consult the server for more information to prepare for subsequent real requests. such as whether the POST method is supported. It is worth noting that:

The browser also sends Access-control-request-method:post and Access-control-request-headers:powered-by-mengxianhui request headers.

Note: The above process is the first time the process of the request, if within 30 seconds repeatedly click the button, you can not see the OPTIONS this process. The execution process is this:

[XHTML]View Plaincopy
  1. Post/preflightedrequests.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. Powered-by-mengxianhui:approve
  11. Content-type:application/xml; charset=UTF-8
  12. Referer:http://www.meng_xian_hui.com:801/crossdomainajax/preflightedrequests.html
  13. Content-length:19
  14. origin:http://www.meng_xian_hui.com:801
  15. Pragma:no-cache
  16. Cache-control:no-cache
  17. <root> Test </root>
  18. http/1.x OK
  19. Date:sun, 14:06:32 GMT
  20. server:microsoft-iis/6.0
  21. X-powered-by:asp.net
  22. x-aspnet-version:2.0.50727
  23. access-control-allow-origin:http://www.meng_xian_hui.com:801
  24. Set-cookie: asp.net_sessionid=qs1c4urxywdbdx55u04pvual; path=/; HttpOnly
  25. Cache-control:private
  26. content-type:text/html; charset=utf-8
  27. Content-length:65

Why is that?  Careful child shoes may be noticed, on the server side there is a line of code Response.AddHeader ("Access-control-max-age", "30"); It is used to set the validity time of the preflight, in seconds. Pay special attention to this point.

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

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.