国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

利用CORS實現POST方式跨域請求數據

2024-09-04 11:43:54
字體:
來源:轉載
供稿:網友

CORS全名Cross-Origin Resource Sharing,顧名思義:跨域分享資源,這是W3C制定的跨站資源分享標準。

目前包括IE10+、chrome、safari、FF都提供了XMLHttpRequest對象對該標準的支持,在更老的IE8中則提供了xDomainRequest對象,部分實現了該標準;

下面是創建request對象的代碼:

  1. var url = "http://www.survivalescaperooms.com/1.php"
  2.  
  3. if (XMLHttpRequest) {  
  4.     var req = new XMLHttpRequest(); 
  5.     // 利用withCredentials屬性來判斷是否支持跨域請求 
  6.     if (!("withCredentials" in req)) { // w3c先行 
  7.         if (window.XDomainRequest) { 
  8.             req = new XDomainRequest(); 
  9.         } 
  10.     } 
  11.     req.open('POST', url, true); 
  12.     req.onload = function (data) { 
  13.         alert(this.responseText); 
  14.     }; 
  15.     req.send(); 

注意xDomainRequest對象只支持http和https協議

在利用XMLHttpRequest對象發POST請求前會發一個options嗅探來確定是否有跨域請求的權限;同時在header頭上帶上Origin信息來指示來源網站信息,服務器響應時需要帶上Access-Control-Allow-Origin頭的值是否和Origin信息相匹配。

header("Access-Control-Allow-Origin: http://localhost"); // *為全部域名

CORS的缺點是你必須能控制服務器端的權限,允許你跨域訪問

設置CORS實現跨域請求

一、使用php代碼實現

  1. # CORS config for php 
  2. # Code by anrip[mail@anrip.com] 
  3.  
  4. function make_cors($origin = '*') { 
  5.  
  6.     $request_method = $_SERVER['REQUEST_METHOD']; 
  7.  
  8.     if ($request_method === 'OPTIONS') { 
  9.  
  10.         header('Access-Control-Allow-Origin:'.$origin); 
  11.         header('Access-Control-Allow-Credentials:true'); 
  12.         header('Access-Control-Allow-Methods:GET, POST, OPTIONS'); 
  13.  
  14.         header('Access-Control-Max-Age:1728000'); 
  15.         header('Content-Type:text/plain charset=UTF-8'); 
  16.         header('Content-Length: 0',true); 
  17.  
  18.         header('status: 204'); 
  19.         header('HTTP/1.0 204 No Content'); 
  20.  
  21.     } 
  22.  
  23.     if ($request_method === 'POST') { 
  24.  
  25.         header('Access-Control-Allow-Origin:'.$origin); 
  26.         header('Access-Control-Allow-Credentials:true'); 
  27.         header('Access-Control-Allow-Methods:GET, POST, OPTIONS'); 
  28.  
  29.     } 
  30.  
  31.     if ($request_method === 'GET') { 
  32.  
  33.         header('Access-Control-Allow-Origin:'.$origin); 
  34.         header('Access-Control-Allow-Credentials:true'); 
  35.         header('Access-Control-Allow-Methods:GET, POST, OPTIONS'); 
  36.  
  37.     } 
  38.  

二、使用nginx配置實現

  1. # CORS config for nginx 
  2. # Code by anrip[mail@anrip.com] 
  3.  
  4. location / { 
  5.  
  6.     set $origin '*'
  7.  
  8.     if ($request_method = 'OPTIONS') { 
  9.  
  10.         add_header 'Access-Control-Allow-Origin' $origin
  11.  
  12.         # 
  13.         # Om nom nom cookies 
  14.         # 
  15.         add_header 'Access-Control-Allow-Credentials' 'true'
  16.         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'
  17.  
  18.         # 
  19.         # Custom headers and headers various browsers *should* be OK with but aren't 
  20.         # 
  21.         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'
  22.  
  23.         # 
  24.         # Tell client that this pre-flight info is valid for 20 days 
  25.         # 
  26.         add_header 'Access-Control-Max-Age' 1728000; 
  27.         add_header 'Content-Type' 'text/plain charset=UTF-8'
  28.         add_header 'Content-Length' 0; 
  29.  
  30.         return 204; 
  31.  
  32.     } 
  33.  
  34.     if ($request_method = 'POST') { 
  35.  
  36.         add_header 'Access-Control-Allow-Origin' $origin
  37.         add_header 'Access-Control-Allow-Credentials' 'true'
  38.         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'
  39.         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'
  40.  
  41.     } 
  42.  
  43.     if ($request_method = 'GET') { 
  44.  
  45.         add_header 'Access-Control-Allow-Origin' $origin
  46.         add_header 'Access-Control-Allow-Credentials' 'true'
  47.         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'
  48.         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'
  49.  
  50.     } 
  51.  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆化县| 新建县| 云阳县| 平度市| 沧源| 三亚市| 宁安市| 泊头市| 广河县| 福海县| 中江县| 韩城市| 行唐县| 炉霍县| 休宁县| 章丘市| 五常市| 临洮县| 西乌珠穆沁旗| 晋州市| 威海市| 都江堰市| 江陵县| 罗定市| 双柏县| 泽州县| 盖州市| 平谷区| 崇信县| 长武县| 山东省| 青州市| 涞水县| 安宁市| 长葛市| 景谷| 拉孜县| 定兴县| 永仁县| 仙游县| 河津市|