国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

所以我知道那裡有很多 CORS 帖子,我只是添加它們,但我找不到任何可以幫助我的答案。所以我正在建立一個(gè)依賴我的 php api 的 Angular 4 應(yīng)用程式。在本地工作沒問題,當(dāng)我將其與位於app.example.com 的應(yīng)用程式和位於api.example.com 的api 一起扔到網(wǎng)域上時(shí),我無法透過我的登錄,因?yàn)槲业玫匠霈F(xiàn)以下錯誤:

XMLHttpRequest 無法載入 http://api.example.com/Account/Login。 對預(yù)檢請求的回應(yīng)未通過存取控制檢查:否 請求中存在「Access-Control-Allow-Origin」標(biāo)頭 資源。因此不允許來源“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
        });
}

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

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

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

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

我透過告訴我的即時(shí)應(yīng)用程式前往本地 api 來調(diào)試 php,並且 php 正在按預(yù)期工作。它會設(shè)定標(biāo)頭並將其放入每個(gè) if 語句中。

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

編輯鏡頭 3 如果我在 .htaccess 而不是 php 中設(shè)定所有標(biāo)頭內(nèi)容,它就會讓我通過。但是,現(xiàn)在我陷入了上面列出的錯誤,這是我在使用郵遞員時(shí)總是遇到的錯誤,但現(xiàn)在是在使用實(shí)際網(wǎng)站時(shí)遇到的錯誤。

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

#

我的回應(yīng)標(biāo)頭是這樣的

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:*

一旦它正常工作,我會將其從 * 更改為僅我的網(wǎng)域。但現(xiàn)在我將其保留為 *。

根據(jù)要求我的標(biāo)頭。

P粉564192131
P粉564192131

全部回覆(2)
P粉952365143

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

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");

在他們之後,我可以忽略選項(xiàng)請求:

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

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

P粉860370921

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

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

現(xiàn)在,對於後端的兩個(gè)請求,您需要確保返回正確的標(biāo)題:content-type、allow-origin、allow-headers 等...

確保在後端的 OPTIONS 請求中,應(yīng)用程式會傳回標(biāo)頭並回傳回應(yīng),而不是繼續(xù)應(yīng)用程式的完整流程。

在「真實(shí)」請求中,您應(yīng)該傳回正確的標(biāo)頭和常規(guī)回應(yīng)正文。

範(fàn)例:

//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 確?!癆ccess-Control-Allow-Origin”不是“*”,它必須設(shè)定正確的網(wǎng)域! (這裡流了很多血:/)

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

  • 如果您使用“Authorization: Bearer”,那麼“Access-Control-Allow-Headers”也應(yīng)包含“Authorization”,否則請求將失敗
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板