@pyHammer

Как разрешить браузеру получать ответы 400 и т.п. через CORS?

Настроил CORS все работает. Однако мне нужно показывать ошибки заполненной формы в SPA мне нужен ответ сервера со статусом 400.
Браузер отдает XMLHttpRequest cannot load localhost/clients. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost' is therefore not allowed access. The response had HTTP status code 400.

Nginx.conf
location / {
		if ($request_method = 'OPTIONS') {
			add_header 'Access-Control-Allow-Origin' 'http://localhost';
			add_header 'Access-Control-Allow-Credentials' 'true';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS';

			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma,Authorization';
			add_header 'Access-Control-Max-Age' 1728000;
			add_header 'Content-Type' 'text/plain charset=UTF-8';
			add_header 'Content-Length' 0;
			return 204;
		}
		if ($request_method = 'POST') {
			add_header 'Access-Control-Allow-Origin' 'http://localhost';
			add_header 'Access-Control-Allow-Credentials' 'true';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS';
			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma,Authorization';
		}
		if ($request_method = 'GET') {
			add_header 'Access-Control-Allow-Origin' 'http://localhost';
			add_header 'Access-Control-Allow-Credentials' 'true';
			add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS';
			add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma,Authorization';
		}


Заранее спасибо!
  • Вопрос задан
  • 699 просмотров
Решения вопроса 1
@pyHammer Автор вопроса
Решил проблему пересборкрй Nginx с установкой модуля headers-more-nginx-module.
Nginx.conf
location / {
		#
		if ($request_method = 'PUT') {
			more_set_headers    "Access-Control-Allow-Origin: http://localhost";
			more_set_headers    "Access-Control-Allow-Credentials: true";
			more_set_headers    "Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS";
			more_set_headers    "Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma,Authorization";
		}
		#
	}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
drugoi
@drugoi
Front-end Developer
Проще сделать Access-Control-Allow-Origin для всех через * и не забыть убрать это, когда код пойдёт на продакшн.
Ещё можно запустить Chrome в режиме выключения web security с помощью --disable-web-security.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы