0

I'm trying to make a post request to server and want to send the token with header .

But then my post request is not working and i can confirm it by because i'm logging in server if that post api got hit or not

and its logging when i don't attach headers and when i attach its not logging anything it's like request never hit up the server.

This code works :

 $.ajax({
    url: 'http://localhost:8080/newUrl',
    type: 'post',
    data: {
        url: myUrl
    },
    dataType: 'json',
    success: function (data) {
        alert(data);
    }
    });

This one doesn't :

 $.ajax({
    url: 'http://localhost:8080/newUrl',
    type: 'post',
    data: {
        url: myUrl
    },headers: {
      Authorization: token
  },
    dataType: 'json',
    success: function (data) {
        alert(data);
    }
});

Also this one doesn't :

$.ajaxSetup({
    headers:{
       'Authorization': token
    }
 });
  $.ajax({
    url: 'http://localhost:8080/newUrl',
    type: 'post',
    data: {
        url: myUrl
    },
    dataType: 'json',
    success: function (data) {
        alert(data);
    }
});

**node.js server CORS related stuff : **

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");
    next();
  });

Console error : Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response

Prashant Bhardwaj
  • 86
  • 1
  • 2
  • 11
  • Most likely this is a cross-domain request, but you neglected to configure the CORS related stuff correctly. You don’t see a POST request on your server side, because the pre-flight OPTIONS request has failed. Pretty sure your browser console should be singing about this, did you bother to check it …? – CBroe Jun 04 '18 at 08:11
  • @CBroe Please read the question carefully!! I have already configured the CORS related stuff that is why i said request without header attached are working only when i attach headers it doesn't work – Prashant Bhardwaj Jun 04 '18 at 08:12
  • _“I have already configured the CORS related stuff”_ - well by the looks of it, most likely not _correctly_ … _“that is why i said request without header attached are working only when i attach headers it doesn't work”_ - and depending on how _specifically_ you configured CORS, that might be exactly the results that are to be expected. Plus, you conveniently left out any response regarding the browser console - so are we to assume that stays completely silent …? – CBroe Jun 04 '18 at 08:16
  • _“I have already configured the CORS related stuff that is why i said request without header attached are working only when i attach headers it doesn't work”_ - that can be read however carefully you like, it doesn’t change the fact that _you_ are drawing the wrong conclusions here. That it works if you do not add this header _only_ means that you configured it to work correctly without this header. But as soon as you add this header, it fails, because you did _not_ configure it correctly _for that_. – CBroe Jun 04 '18 at 08:20
  • @CBroe check the edits , I have attached everything now and also console warning . problem is its not working even though i have Access-Control-Allow-Header – Prashant Bhardwaj Jun 04 '18 at 08:23
  • _“problem is its not working even though i have Access-Control-Allow-Header”_ - problem is that your `Access-Control-Allow-Header` value that you are sending does not contain the header name `Authorization` (so who here needs to _actually_ read carefully, hm?) ... so all you have to do is add it to that list. – CBroe Jun 04 '18 at 08:31
  • @CBroe yeah i figured it out when was rechecking my edits and figured my stupid mistake – Prashant Bhardwaj Jun 04 '18 at 08:32

0 Answers0