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
93,636 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,880 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,880 points
3,191 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,880 points
43,264 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,880 points
7,823 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,510 points
595 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,880 points
21,753 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,880 points
2,647 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,880 points
2,505 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,880 points
41,375 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,880 points
17,973 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,880 points
28,130 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