D
D
Dmitry2016-09-13 05:35:29
Nginx
Dmitry, 2016-09-13 05:35:29

How to allow the browser to receive 400 responses, etc. via CORS?

Configured CORS everything works. However, I need to show filled form errors in SPA, I need server response with status 400.
Browser returns 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';
    }

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-09-13
@pyHammer

Solved the problem of rebuilding Nginx with the installation of the headers-more-nginx-module 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";
    }
    #
  }

N
Nikita Baev, 2016-09-13
@drugoi

It's easier to make Access-Control-Allow-Origin for everyone via * and don't forget to remove it when the code goes into production.
You can also start Chrome in web security off mode with --disable-web-security.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question