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,520 points
103,686 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!! If not then you should enroll with one of our Java certification course.
Thank You!

answered Aug 25, 2020 by Niroj
• 82,840 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 18, 2020 in Java-Script by Niroj
• 82,840 points
3,677 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,840 points
46,077 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,840 points
8,569 views
0 votes
0 answers

How to create a <style> tag with Javascript?

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

Sep 21, 2020 in Java-Script by kartik
• 37,520 points
876 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,840 points
23,097 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,840 points
3,019 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,840 points
2,951 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,840 points
44,898 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,840 points
19,643 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,840 points
29,109 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP