0

I want to send a header to a server using http call POST request. When I send the header only, everything is ok. but when I add some data to the call, I get an error:

http://localhost:3000/test_post. 
Request header field Content-Type is not
allowed by Access-Control-Allow-Headers in preflight response.

I use node.js as the server side language, this is the code for the CORS settings:

app.use(function(req, res, next) {

res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Expose-Headers", "x-auth"); 
res.header("Access-Control-Allow-Headers", "x-auth"); 

next();
});

This is the $http call with angular:

 $http({
    method: 'POST',
    data: {name: 'Dani'},
    url: 'http://localhost:3000/test_post'
    headers: {
        'x-auth': 'some token'
    }
}).then(function successCallback(response) {

    console.log(response.data);

}, function errorCallback(response) {

    console.log('error');
});

I know problem is about the CORS, but I don't know what to modify there, If I remove res.header("Access-Control-Allow-Headers", "x-auth"); from the CORS I can get the data on the server but not the Header.

How can I get them both? Hope you can help me with that, Thank you.

zb22
  • 3,126
  • 3
  • 19
  • 34
  • 1
    Possible duplicate of [Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers](https://stackoverflow.com/questions/25727306/request-header-field-access-control-allow-headers-is-not-allowed-by-access-contr) – lin Aug 15 '17 at 07:58
  • Please search for possible answers before asking. This question has been solved dozens of times. – lin Aug 15 '17 at 07:59
  • @lin it's not the same question, I did put the cors on my server – zb22 Aug 15 '17 at 08:02
  • Please read the answers carefully. The linked question does solve your problem since the error is the same. – lin Aug 15 '17 at 08:06

0 Answers0