Category / Section
How to add authorization header in the Syncfusion Javascript AJAX module
1 min read
This article explains How to add an authorization header in the Syncfusion JavaScript AJAX
The provided example demonstrates how to achieve this by defining an authorization token, setting up an AJAX instance, and customizing the request headers to include the token. This approach ensures that the server can authenticate the client, granting access only to authorized users or applications
Refer the below code snippet
// Define your authorization token (You should replace 'YOUR_TOKEN' with your actual token)
const authToken = 'YOUR_TOKEN';
// Create an AJAX instance
let ajax = new ej.base.Ajax('https://api.example.com/data', 'GET', true);
// Add an event handler for the beforeSend event to set the Authorization header
ajax.beforeSend = function (args) {
// Set the Authorization header with your token
args.headers = {
'Authorization': `Bearer ${authToken}`
};
};
// Perform the AJAX request
ajax.send().then(function (response) {
// Handle the response here
console.log('Response:', response);
}).catch(function (error) {
// Handle any errors here
console.error('Error:', error);
});
Refer to our documentation and online samples for more features. If you have any queries, please let us know in the comments below. You can also contact us through our Support forum or Support ticket. We are happy to assist you!