I need to send token bearer from ajax JQUERY

1

I have a problem ... I have an ajax jquery request to a webapi waiting for a token. I add the Authorization Bearer + Token header but it does not reach the web api ... I expose the code:

        $.ajax({
        url : 'http://localhost:59596/Prueba',
        data : {},
        dataType : 'jsonp',
        success : function(json) {
           debugger;
        },
        beforeSend: function(xhr, settings) { 
            xhr.setRequestHeader('Authorization','Bearer 93854545454545454' );
        },
        error : function(xhr, status) {
            alert('Disculpe, existió un problema');
        },
        complete : function(xhr, status) {
            alert('Petición realizada');
        }
    });

I've also tried it this way:

        $.ajax({
        url : 'http://localhost:59596/Prueba',
        data : {},
        dataType : 'jsonp',
        success : function(json) {
           debugger;
        },
        beforeSend: function(xhr, settings) { 
            xhr.setRequestHeader('Authorization','Bearer 93854545454545454' );
        },
        error : function(xhr, status) {
            alert('Disculpe, existió un problema');
        },
        complete : function(xhr, status) {
            alert('Petición realizada');
        }
    });

In no way comes ... the most curious thing is that since postman, comes

    
asked by Gabriel 06.09.2018 в 05:23
source

1 answer

2

Try this:

$.ajax({
  url : 'http://localhost:59596/Prueba',
  data : {},
  dataType : 'jsonp',
  // Añade un header:
  headers: {'Authorization': 'Bearer 93854545454545454'},
  // El resto del código
  success : function(json) {
     debugger;
  }
  ...
});

According to the jQuery.ajax () documentation, this method has an option called headers which allows add (forgive the redundancy) headers to the calls. According to the documentation, headers is "an additional headers object (of the key / value form) that is sent with the requests using XMLHttpRequest".

    
answered by 06.09.2018 в 05:38