Access-Control-Allow-Origin '*' 不允許/與 Apache2 一起使用

Access-Control-Allow-Origin '*' 不允許/與 Apache2 一起使用

我正在嘗試透過 Javascript 連接到 hood.ie:

hoodie = new Hoodie('http://example.com:6001');

但隨後出現錯誤:A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'xxx' is therefore not allowed access.

和:

hoodie = new Hoodie('http://example.com');

404...

我可以連接到public/admin 的localhost:6001和 : 6002,因此連接埠正在工作(也適用於外部 example.com )

但在 devconsole 中我得到:

OPTIONS: http://example.com:6001/_api/_session'Access-Control-Allow-Origin'當憑證標誌為 true 時,不能在標頭中使用通配符「*」 。起源 'http://example.com' 因此不允許訪問。 `

我已經設定了Apache2:

 <VirtualHost *:80>
    DocumentRoot /var/www
    Header set Access-Control-Allow-Origin *
    Header add Access-Control-Allow-Headers "origin, content-type"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header set Access-Control-Allow-Credentials "false"
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
            Header set Access-Control-Allow-Origin *
            Header set Access-Control-Allow-Credentials "false"
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
    </Directory>
     ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

並嘗試了額外的:

<VirtualHost *:6001>
        Header set Access-Control-Allow-Origin *
        Header set Access-Control-Allow-Credentials "false"
</VirtualHost>

嘗試過:

Header set Access-Control-Allow-Credentials false
Header set Access-Control-Allow-Credentials 'false'
Header set Access-Control-Allow-Credentials true 
...etc

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Origin "example.com"

嘗試了所有可能的變化,但仍然得到相同的錯誤...

但我想知道是否必須將其設置在不同的位置,因為服務www的目錄:6001位於我的主資料夾中的某個位置,而不是 var/www 中,但仍然有效,我不知道 apache 如何處理它,或者知道位置,或hood.ie 是否啟動了自己的網路服務。

答案1

我相信它應該只是:

Header set Access-Control-Allow-Origin "*"

沒有其他 Access-Control-* 標誌,如上所述啟用cors.org

答案2

我會猜測「Hoodie」正在使用 XMLHttpRequest 發送 cookie (withCredentials = "true"),因此您的瀏覽器拒絕來自伺服器的回應,因為它在允許的原始標頭中具有通配符。

相關內容