Place header in ajax get method

Asked

Viewed 857 times

3

Good afternoon, I have the following piece of code:

function getData(){

    var url = "http://teste.com";
    var data = "";

    $.get(url, function(response){

    serverResponse = response;

        for(i in response.content){


            data +='\
            <tr>\
                <td>'+response.content[i].TesteId+'</td>\
                <td>'+response.content[i].TesteNome+'</td>\
                <td>'+response.content[i].TesteEmail+'</td>\
           </tr>';
        }

        $('#corpotabela').empty();
        $('#corpotabela').append(data);

        var width = new Array();
        $(".table-body tr:eq(0)").find('td').each(function (position){
            width[position] = $(this).outerWidth();
        });
        $(".table-header tr").find('th').each(function (position){
            $(this).css("width", width[position]+2);
        });

    });
}

However, to be able to do this request I need to send a token in the header. I can do this using this jQuery method?

  • 1

    Test pass a configuration object with {url: url, beforeSend: function(xhr){xhr.setRequestHeader('X-Token', 'meu-valor');}.

1 answer

1

You’re gonna have to do something like this

$.ajax({
   url : myurl,
   headers: {
        'X-Auth-Token' : token
   });

More information on how to do this, read the doc $.ajax();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.