How do I add authorization headers to the following ajax
request?
$.get(urlAPI + "/api/account/get", function (data) {
alert(data);
}, 'json');
In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action. In the Source field, enter where the content for the header comes from.
For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host. Your load balancer intercepts traffic between the client and your server.
Go to Insert > Header or Footer. Choose from a list of standard headers or footers, go to the list of Header or Footer options, and select the header or footer that you want. Or, create your own header or footer by selecting Edit Header or Edit Footer. When you're done, select Close Header and Footer or press Esc.
When you are using the Axios library and to pass custom headers, you need to construct headers as an object with the key name 'headers'. The 'headers' key should contain an object, here it is Content-Type and Authorization .
You can define ajaxsetup before your request $ .get () to include the authorization in the header
$.ajaxSetup({
headers:{
'Authorization': "auth username and password"
}
});
$.get(urlAPI + "/api/account/get", function (data) {
alert(data);
}, 'json');
based on jquery page $.get() document from version 1.12 there is a settings option to pass the base settings to the method.
jQuery.get( [settings ] )
or
$.get( [settings] )
settings
is a plain object so you can use it like
{
url: 'http://requesturl.io',
data: {},
headers: { 'your-custom-header': 'custom-header-value' }
}
$.ajax({
url: 'foo/bar',
headers: { 'x-my-custom-header': 'some value' }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With