How to add a custom HTTP header to ajax request with javascript

0 votes
Is there any way to add a custom HTTP header to ajax request with javascript?
Aug 25, 2020 in Java-Script by kartik
• 37,510 points
8,880 views

1 answer to this question.

+1 vote

Hello @kartik,

There is different way based on the condition

To add a custom header  to an individual request then just add the headers property:

// Request with custom header
$.ajax({
    url: 'edureka/co',
    headers: { 'custom-header': 'some value' }
});

To add a default header to every request then use $.ajaxSetup():

$.ajaxSetup({
    headers: { 'custom-header': 'some value' }
});

// Sends your custom header
$.ajax({ url: 'edureka/co' });

// Overwrites the default header with a new header
$.ajax({ url: 'edureka/co', headers: { 'some-other-header': 'some value' } });

To add a header to every request then use the beforeSend hook with $.ajaxSetup():

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('custom-header', 'some value');
    }
});
// Sends your custom header
$.ajax({ url: 'edureka/co' });

// Sends both custom headers
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });

Hope it helps!!
Thank You!

answered Aug 25, 2020 by Niroj
• 82,720 points

Related Questions In Java-Script

0 votes
1 answer

How to manage a redirect request after a jQuery Ajax call?

Hii, You can solved this issue by: Adding a ...READ MORE

answered Jun 17, 2020 in Java-Script by Niroj
• 82,720 points
325 views
0 votes
1 answer

How to set cookie value with AJAX request?

Hello @kartik, Basically, ajax request as well as ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,720 points
11,196 views
0 votes
1 answer

How to trigger an ajax request when the user has finished typing in a text box?

Hello @kartik, Use the code below: //setup before functions var ...READ MORE

answered Sep 18, 2020 in Java-Script by Niroj
• 82,720 points
1,094 views
0 votes
0 answers

How to create a <style> tag with Javascript?

I'm looking for a way to insert ...READ MORE

Sep 20, 2020 in Java-Script by kartik
• 37,510 points
52 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
7,246 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
821 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
641 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
7,659 views
0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,720 points
8,944 views
0 votes
1 answer

How to manage a redirect request after a jQuery Ajax call?

Hii kartik, You can resolved this issue like ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,720 points
12,054 views