willie willie - 6 months ago 20
Ajax Question

How to Call ASP.NET Web API base on Ajax with GET/POST and Token key

I know how to use Ajax with GET/POST data like as following code but I don’t know how to use it with Token key (Has got a Token key)

$("#read1").click(function () {
$.support.cors = true;
$.ajax({
crossDomain: true,
url: 'http://localhost:65370/api/travels',
type: 'GET',
cache: false,
error: function (xhr, status, errorThrow) {
},
complete: function (xhr) {
},
success: function (data) {
}
});
});

$("#create1").click(function () {
var person = {
"travel_id": 4
};
$.ajax({
},
type: "post",
url: 'http://localhost:65370/api/travels',
datatype: "json",
contenttype: "application/json; charset=utf-8",
data: person,
success: function (data) {
},
error: function (xhr, status, errorThrow) {
}
});
});

Answer

I have found the method as following code

    //you just need to put it in Ajax content 
    headers:{
                'Authorization': 'Bearer ' + token
            }

When you got the token key you can add the code in the “Headers”.