Monday 5 June 2017

How to get headers element in node js

I would like to get headers element in server side with node js I try these two codes in client side:

CODE 1

 var token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ayI6IjU5Mjg0YjQ0MzNhNjU2MmRlNDI1ZGU5MCIsImlhdCI6MTQ5NjY5NjA1NiwiZXhwIjoyOTkzMzk1NzEyfQ.BQskPVT-h-Io4p3Hqraq2qRmVlp_pWAEw5rnIIEA4vk';
    $.ajax({
        url: 'YourRestEndPoint',
        headers: {
            'Authorization':'Basic' + token
        },
        method: 'POST',
        dataType: 'json',
        data: YourData,
        success: function(data){
          console.log('succes: '+data);
        }
      });

Code 2:

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         beforeSend: function(xhr){xhr.setRequestHeader(                               
          'Authorization':'Basic' + token);
         },
         success: function() { alert('Success!' + authHeader); }
});

And here the code server:

res.header("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.header("Access-Control-Allow-Headers", "Authorization");

console.log(req.headers['authorization']);

I can't get the token in the server side.



via ahmed

No comments:

Post a Comment