V
V
Vitaly2019-10-07 11:29:01
Nginx
Vitaly, 2019-10-07 11:29:01

Why do post requests fail, but GET requests do?

Probably a boring question, but I'm at an impasse.
There is a remote test server on debian, NGINX is configured on it, which proxies the necessary requests to the local node express server service (not docker).
One server on localhost:3001 to work with the database, everything is fine here, all REST requests go through (POST, GET ....).
And the second one on localhost:3002 for uploading images. Here I have a problem with CORS
Error in the browser (Local page on VUE (npm run serve) with axios requests)

Access to XMLHttpRequest at ' https://myTestServer/uploadImg ' from origin ' localhost:8080 ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

I already tried everything in express:
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    next();
});

And I used the module https://www.npmjs.com/package/cors
app.use(cors())
It doesn't help... :(
It's interesting that there are no problems on the localhost:3001 service with the same cors settings. I
also see the first request in the NGIX logs when trying to download images through the browser OPTIONS
"OPTIONS /uploadImg HTTP/1.1" 301 169 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" "-"

What could be the problem ?
PS tests with POSTMAN pass successfully, as well as GET (if you just open the page in the browser)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy, 2019-10-07
@Scorpiored88

Found the problem.
From the client, I sent requests to

' https://myTestServer/uploadImg ' 
so I get an error
But if I send a request with a slash at the end
https://mytestserver/uploadimg/
this is how it works
Although on the server
app.post('/uploadImg', async(req, res) => {

Perhaps because in nginx
location /uploadimg/
although it is strange, since the first service with requests without a slash at the end works ... files are not accepted there

V
vadimMalovaniy, 2019-10-07
@vadimMalovaniy

In Access-Control-Allow-Headers try adding Redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question