1

I am using the following to do a http GET:

$.ajax({
    type: 'GET',
    url: server + '/hello',
    dataType: 'json',
    async: false,
    xhrFields: {
        withCredentials: true
    },

    success: function(data){
        if(data.connected){
    },
    error: function(a, b, c){
    }
});

I see these are set in the headers:

 Accept:application/json, text/javascript, */*; q=0.01
 Accept-Encoding:gzip,deflate,sdch
 Accept-Language:en-US,en;q=0.8
 Authorization:Basic bGlkerrdN1NDpTdW55ytXIwMA==
 Connection:keep-alive 
 Cookie:OBBasicAuth=fromCache; 
 ObSSOCookie=
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?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • **async:false**? Please don't do that... it's deprecated and locks up the UI. Also, what does get the Authorization part mean? You want to inspect existing headers? How was that header set in the first place? You probably are calling https://api.jquery.com/jquery.ajaxsetup/ somewhere – Ruan Mendes Apr 25 '16 at 18:26
  • Don't make synchronous requests. Change async: false to async: true or remove this property. – Jakub Rożek Apr 25 '16 at 18:27

2 Answers2

0

Here is an example how to set and get header in ajax call

$.ajax({
            type:"POST",
            beforeSend: function (request)
            {
                request.setRequestHeader("Token", authorizationToken);
            },
            url: "entities",
            data: "json=" + escape(JSON.stringify(createRequestObject)),
            processData: false,
             success: function(data, textStatus, request){
                  alert(request.getResponseHeader('some_header'));
             },
    });
codeGig
  • 1,024
  • 1
  • 8
  • 11
  • This is how to set a header, the OP seems to want to be able to inspect existing headers? – Ruan Mendes Apr 25 '16 at 18:27
  • I am not setting the headers. It is set by the browser. Is there a way to get the Authorization? – Pritam Banerjee Apr 25 '16 at 18:28
  • i have changed code, you can see it in ajax success. success: function(data, textStatus, request){ – codeGig Apr 25 '16 at 18:28
  • @PritamBanerjee It's not the browser that is setting it on its own... You have to configure it and tell it what to send. – Ruan Mendes Apr 25 '16 at 18:30
  • There is a security login, where I enter my uid and pwd. Then it redirects to my url. Is there a way where I can get this? – Pritam Banerjee Apr 25 '16 at 18:33
  • are you calling an api where basic authentication is required? please clear your question. – codeGig Apr 25 '16 at 18:39
  • @codeGig Yes. Then it let's you use the api. I can see the headers after I sign in with my email and password in the browser. But I cannot get that in the ajax request. Is there a way to get it? – Pritam Banerjee Apr 25 '16 at 19:17
0

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.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108