Ms Anis
Ms Anis

Reputation: 11

Hi, I want to get the data from a json file, but I can't get it

I have an api in nodejs to getter data I need an authorization with token, but I don't know how to do it?

$(document).ready(function() {
    $.getJSON("https://skillz-api.herokuapp.com/users", function(data) {
        $.each(data, function() {
            $("ul").append("<li>" + this['name'] + "</li><li>Age: " + this['age'] + '</li><button type="button" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-ok"></span>Valider</button>&nbsp; <
            button type = "button" class = "btn btn-default btn-sm" > < span class = "glyphicon glyphicon-remove" > < /span>Non valider</button > ');
        });
    });
}); 

My error is GET https://skillz-api.herokuapp.com/users 401 (Unauthorized)

Thanks

Upvotes: 1

Views: 65

Answers (1)

Caio Saldanha
Caio Saldanha

Reputation: 1090

First you must understand that an Authorization token must be passed by the GET Request's Headers, see: https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Headers for more information.

You can build a jquery ajax request with headers, like this:

$.ajax("https://skillz-api.herokuapp.com/users", {
  headers: { "Authorization": "Your token here" }
}).then( (response) => {
    // do whatever you want here
});

Upvotes: 3

Related Questions