PHP中的CORS問題:預檢請求的回應未通過。允許來源
P粉564192131
P粉564192131 2023-10-21 09:35:29
0
2
639

所以我知道那裡有很多 CORS 帖子,我只是添加它們,但我找不到任何可以幫助我的答案。所以我正在建立一個依賴我的 php api 的 Angular 4 應用程式。在本地工作沒問題,當我將其與位於app.example.com 的應用程式和位於api.example.com 的api 一起扔到網域上時,我無法透過我的登錄,因為我得到出現以下錯誤:

XMLHttpRequest 無法載入 http://api.example.com/Account/Login。 對預檢請求的回應未通過存取控制檢查:否 請求中存在「Access-Control-Allow-Origin」標頭 資源。因此不允許來源“http://app.example.com” 訪問。

我的 php 程式碼如下所示:

$http_origin = $_SERVER['HTTP_ORIGIN'];

$allowed_domains = array(
    'http://example.com',
    'https://example.com',
    'http://app.example.com',
    'https://app.example.com',
    'http://www.example.com',
    'https://www.example.com'
);

if (in_array(strtolower($http_origin), $allowed_domains))
{  
    // header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Origin: $http_origin");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
        header("Access-Control-Allow-Headers: Authorization, Content-Type,Accept, Origin");
    exit(0);
}

我的 Angular 帖子如下所示:

public login(login: Login): Observable<LoginResponse> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization', 'Basic ' + btoa(login.Username + ':' + login.Password));
    return  this.http.post(this.apiBaseUrl + '/Account/Login', "grant_type=client_credentials", { headers: headers })
        .map(response => {
            // code
        });
}

如果我透過郵差運行請求(這不會影響 CORS),我會得到:

{ "error": "invalid_client", "error_description": "Client credentials were not found in the headers or body" }

我嘗試將 origin 設定為 '*' 只是為了測試並查看這是否是問題的核心,但它仍然以相同的方式失敗。

編輯# 只要根據以下資訊進行更新。更改標頭中的大小寫沒有任何效果,從 if 語句中提取程式碼也沒有任何效果。

我透過告訴我的即時應用程式前往本地 api 來調試 php,並且 php 正在按預期工作。它會設定標頭並將其放入每個 if 語句中。

編輯鏡頭 2 我確實需要一些幫助,如果有人有任何想法,我將非常感激。

編輯鏡頭 3 如果我在 .htaccess 而不是 php 中設定所有標頭內容,它就會讓我通過。但是,現在我陷入了上面列出的錯誤,這是我在使用郵遞員時總是遇到的錯誤,但現在是在使用實際網站時遇到的錯誤。

{"error":"invalid_client","error_description":"在標頭或正文中找不到客戶端憑證"}

#

我的回應標頭是這樣的

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:authorization, content-type, accept, origin
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*

一旦它正常工作,我會將其從 * 更改為僅我的網域。但現在我將其保留為 *。

根據要求我的標頭。

P粉564192131
P粉564192131

全部回覆(2)
P粉952365143

在我的類似案例中,Angular 前端和 Php 後端幫助了下面的程式碼。首先我發送一個標頭:

header("Access-Control-Allow-Origin: http://localhost:4200");   
header("Content-Type: application/json; charset=UTF-8");    
header("Access-Control-Allow-Methods: POST, DELETE, OPTIONS");    
header("Access-Control-Max-Age: 3600");    
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

在他們之後,我可以忽略選項請求:

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {    
   return 0;    
}

這個方法幫助我處理 Angular 中的「post」和「delete」嵌入請求方法。

P粉860370921

好吧,我最近遇到了類似的問題,我只在後端解決了所有問題,沒有 .htaccess 的東西。

當瀏覽器發送跨伺服器請求時,它首先發送 OPTIONS 請求以確保其有效並且可以發送「真實」請求。 當它從 OPTIONS 獲得正確且有效的回應後,才會發送「真正的」請求。

現在,對於後端的兩個請求,您需要確保返回正確的標題:content-type、allow-origin、allow-headers 等...

確保在後端的 OPTIONS 請求中,應用程式會傳回標頭並回傳回應,而不是繼續應用程式的完整流程。

在「真實」請求中,您應該傳回正確的標頭和常規回應正文。

範例:

//The Response object
    $res = $app->response;

    $res->headers->set('Content-Type', 'application/json');
    $res->headers->set('Access-Control-Allow-Origin', 'http://example.com');
    $res->headers->set('Access-Control-Allow-Credentials', 'true');
    $res->headers->set('Access-Control-Max-Age', '60');
    $res->headers->set('Access-Control-Allow-Headers', 'AccountKey,x-requested-with, Content-Type, origin, authorization, accept, client-security-token, host, date, cookie, cookie2');
    $res->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

    if ( ! $req->isOptions()) {
        // this continues the normal flow of the app, and will return the proper body
        $this->next->call();
    } else {
        //stops the app, and sends the response
        return $res;
    }

要記住的事:

  • 如果您使用:「Access-Control-Allow-Credentials」= true 確保“Access-Control-Allow-Origin”不是“*”,它必須設定正確的網域! (這裡流了很多血:/)

  • #定義您將在「Access-Control-Allow-Headers」中獲得的允許標頭 如果您不定義它們,請求將失敗

  • 如果您使用“Authorization: Bearer”,那麼“Access-Control-Allow-Headers”也應包含“Authorization”,否則請求將失敗
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!