S
S
Sashqa2019-11-15 11:24:22
JavaScript
Sashqa, 2019-11-15 11:24:22

CORS blocks requests, what to do?

I am sending all the headers in the request

const headers = new HttpHeaders({
  'Content-Type':  'application/xml',
  'Accept':  'application/xml',
  'Response-Type': 'text',
  'Access-Control-Allow-Origin': '*',
  'SOAPAction': '',
})
console.log(headers)
return this.http.post('http://site.ru', query, { headers }).subscribe(res => {
  console.log('res', res);
}, error => console.log('error', error));

On the back, everyone added
Response Headers too
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 1728000
Age: 0
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/plain charset=UTF-8
Date: Fri, 15 Nov 2019 08:19:38 GMT
Proxy-Connection: Keep-Alive
Server: nginx/1.12.2
Via: 1.1 forcepoint-wcg.soglasie.ru, 1.1 TMG-NODE1

But the error is still there
Access to XMLHttpRequest at 'http://site.ru' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Dozmorov, 2019-11-15
@Dozalex

It looks like you need to add the access-control-allow-origin header to the Access-Control-Allow-Headers list

P
Pavel Didenko, 2019-11-15
@Dasslier

If you are using Chrome, this might be the reason. Install the CORS browser extension

A
Alexander Aksentiev, 2019-11-15
@Sanasol

in preflight response.

He makes the OPTIONS request first, but the headers in your GET / POST didn’t bother him, he hasn’t reached them yet.

R
Revenant78, 2019-12-02
@Revenant78

create a proxy.config.json in the root directory of the project and write the config

{
  "/api": {
    "target": "http://localhost:9000",
    "secure": false
  }
}

in package.json run the project with npm run start

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question