關於上傳檔案的跨域問題

來源:互聯網
上載者:User

標籤:頁面   xmlhttp   ken   user   ati   傳輸   use   過程   缺點   

在進行新架構開發的過程中,需要自訂頁面組件實現脫離表單的檔案(圖片)上傳,考慮過wex5內建的attachmentsimple的自訂寫法很難受,就改用了第三方外掛程式webuploader來實現選擇檔案後調用服務端的上傳檔案介面實現自動上傳。

中間遇到過跨域問題,即服務端所在介面網域名稱與外掛程式包(前端)不在同一網域名稱下,但是由于格式是檔案,所以必須採用post傳輸方式

解決方案:

利用CORS實現POST方式跨域請求資料

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

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

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

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

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

設定CORS實現跨域請求

一、使用php代碼實現(寫在介面開始位置)

 
    $request_method = $_SERVER[‘REQUEST_METHOD‘];
 
    if ($request_method === ‘OPTIONS‘) {
 
        header(‘Access-Control-Allow-Origin:‘.$origin);
        header(‘Access-Control-Allow-Credentials:true‘);
        header(‘Access-Control-Allow-Methods:GET, POST, OPTIONS‘);
 
        header(‘Access-Control-Max-Age:1728000‘);
        header(‘Content-Type:text/plain charset=UTF-8‘);
        header(‘Content-Length: 0‘,true);
 
        header(‘status: 204‘);
        header(‘HTTP/1.0 204 No Content‘);
 
    }
 
    if ($request_method === ‘POST‘) {
 
        header(‘Access-Control-Allow-Origin:‘.$origin);
        header(‘Access-Control-Allow-Credentials:true‘);
        header(‘Access-Control-Allow-Methods:GET, POST, OPTIONS‘);
 
    }
 
    if ($request_method === ‘GET‘) {
 
        header(‘Access-Control-Allow-Origin:‘.$origin);
        header(‘Access-Control-Allow-Credentials:true‘);
        header(‘Access-Control-Allow-Methods:GET, POST, OPTIONS‘);
 
    }
 

二、使用nginx配置實現


location / {
 
    set $origin ‘*‘;
 
    if ($request_method = ‘OPTIONS‘) {
 
        add_header ‘Access-Control-Allow-Origin‘ $origin;
 
        #
        # Om nom nom cookies
        #
        add_header ‘Access-Control-Allow-Credentials‘ ‘true‘;
        add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘;
 
        #
        # Custom headers and headers various browsers *should* be OK with but aren‘t
        #
        add_header ‘Access-Control-Allow-Headers‘ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type‘;
 
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header ‘Access-Control-Max-Age‘ 1728000;
        add_header ‘Content-Type‘ ‘text/plain charset=UTF-8‘;
        add_header ‘Content-Length‘ 0;
 
        return 204;
 
    }
 
    if ($request_method = ‘POST‘) {
 
        add_header ‘Access-Control-Allow-Origin‘ $origin;
        add_header ‘Access-Control-Allow-Credentials‘ ‘true‘;
        add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘;
        add_header ‘Access-Control-Allow-Headers‘ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type‘;
 
    }
 
    if ($request_method = ‘GET‘) {
 
        add_header ‘Access-Control-Allow-Origin‘ $origin;
        add_header ‘Access-Control-Allow-Credentials‘ ‘true‘;
        add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘;
        add_header ‘Access-Control-Allow-Headers‘ ‘DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type‘;
 
    }
 
}

 

關於上傳檔案的跨域問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.