0

I am sending an ajax call to the server to sign up the user

    $.ajax({
    url: `${baseUrl}/users`,
    type: 'POST',
    data: data,
    success: function(data) {
       console.log(data);
       $('#signUpModal').modal('hide');         
    },
    error: function(e) {
        console.log(e);
    }
});

Server side(snipet):

        user.save().then(()=>{
        return user.generateAuthToken();
    }).then((token)=>{
        res.header('x-auth', token).send(user);
    }).catch((e)=>{
        res.status(400).send(e)
    });

The code does save user to the database, but doesn't set the header.

Note:It sets the header when i am using Postman, so it must be the ajax call that causes the problem? Note2:it works if i send the data without jQuery. (with good old plain form), but i am curios about the solution so i won't delete my question.

enter image description here

Thank you in advance for your help.

elwindly
  • 45
  • 1
  • 7
  • Have a look at this [answer](http://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header#answer-4236041) – rckrd Dec 27 '16 at 13:57
  • Ok, so i can get the headers and save it to somewhere(e.g :localstrorage) but i am not able the set the response header in the browser? I updated my question with a picture where i want to set the header. – elwindly Dec 27 '16 at 16:54
  • goggle "set headers jquery ajax" and you will most likely find a solution – rckrd Dec 27 '16 at 17:04
  • I know, i had found thousands of pages explaining how to send headers with ajax request, but i would like to set headers from the server side (express.js) which can be done with res.header ( and it works when i don't use jquery). I understand that i can work around this problem by storing somewhere the information and send it back with ajax again but that seems like a bad idea. – elwindly Dec 27 '16 at 17:13
  • In the picture above the server have set the x-auth header, so as far as I can see your sever does what you want – rckrd Dec 27 '16 at 17:20
  • that is the without jquery picture to show you where i want to set the headers to avoid confusion. – elwindly Dec 27 '16 at 19:37

0 Answers0