2017-02-20 31 views
0

Я разрабатываю мобильное приложение с кросс-платформой, используя Magento 2 rest apis.I включил CORS на моем сервере Apache.Проблемы, связанные с CORS на сервере 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> 

Когда я называю APIs из моих browser.I я получаю ответ успеха.

enter image description here

Но в моей консоли, показывая CORS связанных вопросов.

enter image description here

Пожалуйста, помогите мне в этом?

ответ

1

Удалите Header always set Access-Control-Allow-Origin "*" с вашего 000-default.conf, а также удалите Header always set Access-Control-Allow-Headers.

Это вызывает множество Access-Control-Allow-Origin & Access-Control-Allow-Headers заголовки, которые будут отправлены в ответ. (См Response Headers части в Devtools изображении. Если браузер несколько заголовков ответа с тем же именем, они сливаются значение заголовка.)

Так, во всяком случае, некоторые другие части серверной среды уже установка эти заголовки. Вам либо нужно отключить все, что добавляет их, либо просто добавить любые другие необходимые заголовки, которые еще не добавлены.

Например, в ответе есть только один Access-Control-Allow-Headers, и только один Access-Control-Max-Age, так что предположительно ваша конфигурация Apache - единственная вещь, добавляющая их.

+0

Удален эти lines.Then я получаю ответ «Сервер обнаружил внутреннюю ошибку или некорректный и не смогли выполнить ваш запрос. » – Muhsin

+0

Тогда кажется, что вы, возможно, случайно ввели некоторую другую синтаксическую ошибку, когда вы сделали изменение - потому что просто удалить эти директивы не приведет к тому, что вы столкнетесь с этой ошибкой. – sideshowbarker