AJAX(XMLHttpRequest)進行跨域請求方法詳解(三)

注意:如下代碼請在Firefox 3.五、Chrome 3.0、Safari 4以後的版本中進行測試。IE8的實現方法與其餘瀏覽不一樣。javascript

 

3,帶驗證信息的請求html

 

身份驗證是Web開發中常常遇到的問題,在跨域請求中,默認狀況下是不發送驗證信息的。要想發送驗證信息,須要進行withCredentials 屬性,下面就是一個簡單請求的例子:java

 

[xhtml]  view plain copy print ?
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟憲會之AJAX跨域請求測試</title>
  6. </head>
  7. <body>
  8. <inputtype='button'value='開始測試'onclick='crossDomainRequest()'/>
  9. <divid="content"></div>
  10. <mce:scripttype="text/javascript"><!--
  11. var xhr =new XMLHttpRequest();
  12. var url ='http://dotnet.aspx.cc/RequestsWithCredentials.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML ="開始進行請求……";
  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 ="不能建立 XMLHttpRequest。";
  22. }
  23. }
  24. function handler(evtXHR) {
  25. if (xhr.readyState == 4) {
  26. if (xhr.status == 200) {
  27. var response =xhr.responseText;
  28. document.getElementById("content").innerHTML ="結果:" + response;
  29. } else {
  30. document.getElementById("content").innerHTML += "<br/>執行狀態 status:" + xhr.status;
  31. }
  32. }
  33. else {
  34. document.getElementById("content").innerHTML += "<br/>執行狀態 readyState:" + xhr.readyState;
  35. }
  36. }
  37. // --></mce:script>
  38. </body>
  39. </html>
[xhtml]  view plain  copy
 
 print?
  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>孟憲會之AJAX跨域請求測試</title>  
  6. </head>  
  7. <body>  
  8.   <input type='button' value='開始測試' 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 = "開始進行請求……";  
  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 = "不能建立 XMLHttpRequest。";  
  22.       }  
  23.     }  
  24.     function handler(evtXHR) {  
  25.       if (xhr.readyState == 4) {  
  26.         if (xhr.status == 200) {  
  27.           var response = xhr.responseText;  
  28.           document.getElementById("content").innerHTML = "結果:" + response;  
  29.         } else {  
  30.           document.getElementById("content").innerHTML += "<br/>執行狀態 status:" + xhr.status;  
  31.         }  
  32.       }  
  33.       else {  
  34.         document.getElementById("content").innerHTML += "<br/>執行狀態 readyState:" + xhr.readyState;  
  35.       }  
  36.     }  
  37. // --></mce:script>  
  38. </body>  
  39. </html>  

 

 

 

點擊「開始測試」,咱們能夠檢測到下面的請求執行過程:跨域

 

[xhtml]  view plain copy print ?
 
  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 200 OK
  13. Date: Sun, 10 Jan 2010 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
[xhtml]  view plain  copy
 
 print?
  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 200 OK  
  13. Date: Sun, 10 Jan 2010 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  

 

 

 

從上面的響應中能夠看出,Cookie 是會隨請求一塊兒發送的。若是咱們屢次點擊測試按鈕,則能夠看到請求和響應的結果是這樣的:瀏覽器

 

[xhtml]  view plain copy print ?
 
  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 200 OK
  14. Date: Sun, 10 Jan 2010 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
[xhtml]  view plain  copy
 
 print?
  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 200 OK  
  14. Date: Sun, 10 Jan 2010 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  

 

 

 

注意 Cookie: ASP.NET_SessionId=fn2zf0zq1cuwgf45fm5fw145; visit=2 這一行,訪問計數器已經被一塊兒發送到服務器。服務器

 

4,IE8 中的實現方法app

IE8已經開始支持跨域訪問資源了,可是,IE8提供的功能還比較簡單,能夠進行簡單的請求,下面是一個使用的例子:測試

 

[xhtml]  view plain copy print ?
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>孟憲會之AJAX跨域請求測試</title>
  6. </head>
  7. <body>
  8. <inputtype='button'value='開始測試'onclick='crossDomainRequest()'/>
  9. <divid="content"></div>
  10. <mce:scripttype="text/javascript"><!--
  11. var xhr =new XDomainRequest();
  12. var url ='http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx';
  13. function crossDomainRequest() {
  14. document.getElementById("content").innerHTML ="開始……";
  15. if (xhr) {
  16. xhr.open('GET', url);
  17. xhr.onload =handler;
  18. xhr.send();
  19. } else {
  20. document.getElementById("content").innerHTML ="不能建立 XDomainRequest";
  21. }
  22. }
  23. function handler(evtXHR) {
  24. document.getElementById("content").innerHTML ="結果:" + xhr.responseText;
  25. }
  26. // --></mce:script>
  27. </body>
  28. </html>
[xhtml]  view plain  copy
 
 print?
  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>孟憲會之AJAX跨域請求測試</title>  
  6. </head>  
  7. <body>  
  8.   <input type='button' value='開始測試' 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 = "開始……";  
  15.       if (xhr) {  
  16.         xhr.open('GET', url);  
  17.         xhr.onload = handler;  
  18.         xhr.send();  
  19.       } else {  
  20.       document.getElementById("content").innerHTML = "不能建立 XDomainRequest";  
  21.       }  
  22.     }  
  23.     function handler(evtXHR) {  
  24.       document.getElementById("content").innerHTML = "結果:" + xhr.responseText;  
  25.     }  
  26. // --></mce:script>  
  27. </body>  
  28. </html>  

 

 

 

 

另外,IE8的實現方法與其餘瀏覽器不一樣。更多內容請參考 XDomainRequest 對象,地址是:
http://msdn.microsoft.com/zh-cn/library/cc288060(VS.85).aspx

最後,願意測試的朋友能夠訪問這個 http://dotnet.aspx.cc/SimpleCrossSiteRequests.aspx 地址進行「簡單請求」的測試,本頁面容許任何地址進行跨域訪問。(很差意思,我的網站可能被河蟹了)網站

相關文章
相關標籤/搜索