How to change http header using () .get and () .post ()?

1

In a very brief way I need to send a jwt token in the http header of the requests, how can I change the headers of the jQuery requests $.post() and $.get() ?

Example of how I do it using curl from the prompt:

curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcHAudGVjbWFuLmNvbSIsInVzZXJuYW1lIjoiSmVhbiBGcmVpdGFzIiwiZW1haWwiOiJoZW5yaXF1ZWszQGxpdmUuY29tIiwiYWNsIjpbInJoIiwiZmluYW5jZWlybyIsImNvbWVyY2lhbCIsImVuZ2VuaGFyaWEiLCJhZG1pbmlzdHJhdGl2byJdfQ==.kq4nJJYBTbjWwtbmu\/wB+EXilDHHPr8es6mXpsXEVMs=" http://meuapp.com/jwt
    
asked by anonymous 10.01.2018 / 20:46

1 answer

0

You can use something like:

$.ajax({
  url: "http://meuapp.com/jwt",
  dataType: 'json',
  success: function(data, status) {
    return console.log("Dados", data);
  },
  beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + token); } 
});

Jquery ajax setting

    
10.01.2018 / 20:54