Pritam Banerjee Pritam Banerjee - 1 year ago 85
Javascript Question

How to get the Ajax header fields in javascript?

I am using the following to do a http GET:

type: 'GET',
url: server + '/hello',
dataType: 'json',
async: false,
xhrFields: {
withCredentials: true

success: function(data){
error: function(a, b, c){

I see these are set in the headers:

Accept:application/json, text/javascript, */*; q=0.01
Authorization:Basic bGlkerrdN1NDpTdW55ytXIwMA==
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.10 Safari/537.36
Response Headersview source

IS there a way in Javascript or JQuery I can get the Authorization part?

Answer Source

Could not get that to work in JavaScript.

Got that working on the server side, where I grabbed the details from the headers, by using @context HttpServletRequest. Then grabbed the headers and sent the required parameters back to the client.

That was the only way to do it, as far as I read it from other sources, because JavaScript does not have access to these cookies and headers which are set by the browser.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download