簡體   English   中英

在Drupal中啟用和配置cors

[英]Enable and configure cors in Drupal

我正在調試Drupal問題,將其用作無頭api服務器。

當有人在JavaScript應用程序中提交表單時,我必須向Drupal的聯系表單REST Endpoint發出POST請求。

當我從REST客戶端測試該API時,該API正常工作。

但是,當我在瀏覽器中從JavaScript運行相同的代碼時,出現了cors問題。

我確保在sites/default/default.services.yml文件中啟用了cors。

但是這些變化沒有生效。

以下是sites/default/default.services.yml文件的相關內容

  cors.config:
    enabled: true
    # Specify allowed headers, like 'x-allowed-header'.
    allowedHeaders: ['*']
    # Specify allowed request methods, specify ['*'] to allow all possible ones.
    allowedMethods: ['*']
    # Configure requests allowed from specific origins.
    allowedOrigins: ['*']
    # Sets the Access-Control-Expose-Headers header.
    exposedHeaders: true
    # Sets the Access-Control-Max-Age header.
    maxAge: false
    # Sets the Access-Control-Allow-Credentials header.
    supportsCredentials: false

我在瀏覽器中遇到以下錯誤。

Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.

我試圖將sites/default/default.services.yml更改為以下內容,但仍然遇到相同的問題。

allowedHeaders: ['x-csrf-token', 'authorization', 'content-type', 'accept', 'origin', 'x-requested-with']

將文件名從default.services.yml更改為services.yml
同樣,暴露的標題屬性應該是標題名稱的數組,或者為false,但絕不能為“ true”。
src: https//www.drupal.org/node/2715637

感謝大家的投入。

它似乎不起作用的原因是因為Drupal安裝中已經啟用了舊的“ Drupal Cors”模塊,該模塊具有優先權。

禁用它之后,我對services.yml所做的更改開始生效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM