While using the jQuery.ajax header function for including headers for the token authentication for API requests, you can use the attribute of “header” for adding the custom headers.
Here is the example given:
// Your authentication token (replace ‘YOUR_TOKEN’ with the actual token)
Var authToken = ‘YOUR_TOKEN’;
// AJAX request using jQuery
$.ajax({
url: ‘https://api.example.com/endpoint’, // Your API endpoint
type: ‘GET’, // Method type (GET, POST, etc.)
headers: {
‘Authorization’: ‘Bearer ‘ + authToken // Adding the authentication token in the ‘Authorization’ header
// You might have other headers as needed },
Success: function(response) {
// Handle successful response
Console.log(‘Response:’, response);
},
Error: function(xhr, status, error) {
// Handle error
Console.error(‘Error:’, error);
}
});
In the above example given, the “authorization” header is added to the AJAX request by using the attributes of the “header” with the “Bearer” value after the token of authentication. You can adjust the name of the header and its value as per your needs and requirements.