2017-02-20 149 views
0

相關問題CORS我開發使用的Magento 2其餘apis.I一個跨平臺的移動應用程序已經啓用了CORS在我的Apache服務器。在Apache服務器

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80> 
     # The ServerName directive sets the request scheme, hostname and port that 
     # the server uses to identify itself. This is used when creating 
     # redirection URLs. In the context of virtual hosts, the ServerName 
     # specifies what hostname must appear in the request's Host: header to 
     # match this virtual host. For the default virtual host (this file) this 
     # value is not decisive as it is used as a last resort host regardless. 
     # However, you must set it for any further virtual host explicitly. 
     #ServerName www.example.com 

     ServerAdmin [email protected] 
     DocumentRoot /var/www/html 
     # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
     # error, crit, alert, emerg. 
     # It is also possible to configure the loglevel for particular 
     # modules, e.g. 
     #LogLevel info ssl:warn 

     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 

     # For most configuration files from conf-available/, which are 
     # enabled or disabled at a global level, it is possible to 
     # include a line for only one particular virtual host. For example the 
     # following line enables the CGI configuration for this host only 
     # after it has been globally disabled with "a2disconf". 
     #Include conf-available/serve-cgi-bin.conf 
     # Always set these headers. 
     Header always set Access-Control-Allow-Origin "*" 
     Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT" 
     Header always set Access-Control-Max-Age "1000" 
     Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token" 

     # Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request. 
     RewriteEngine On 
     RewriteCond %{REQUEST_METHOD} OPTIONS 
     RewriteRule ^(.*)$ $1 [R=200,L] 
</VirtualHost> 

當我打電話從我browser.I API的我得到了成功響應。

enter image description here

但相關的問題我的控制檯顯示CORS。

enter image description here

請幫我在這?

回答

1

從您的000-default.conf中刪除Header always set Access-Control-Allow-Origin "*",並刪除Header always set Access-Control-Allow-Headers

這些導致多個Access-Control-Allow-Origin & Access-Control-Allow-Headers標題將在響應中發送。 (見devtools圖像響應頭一部分。當瀏覽器的多個響應頭具有相同的名稱,它們融合頭的值。)

所以,無論如何,在服務器環境中的其他部分已經被設置這些標題。你或者需要禁用任何其他的添加,或者你只需​​添加任何其他必要的頭文件還沒有被添加。

例如,響應中只有一個Access-Control-Allow-Headers,並且只有一個Access-Control-Max-Age,所以推測您的Apache配置是添加這些配置的唯一方法。

+0

刪除了這些lines.Then我得到的迴應「服務器遇到一個內部錯誤或配置錯誤,無法完成您的請求。 」 – Muhsin

+0

然後好像你可能無意中介紹了一些其他的語法錯誤時,你所做的更改 - 因爲只是刪除這些指令不會導致您遇到該錯誤。 – sideshowbarker