7

I have the need to set the 'Authorization' request header to the httpXMLRequest. On the grid definition I have tried to set via ajaxGridOptions like the following:

 ajaxGridOptions: { Authorization: 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6=' } 

and use the beforeSend event like the following:

   beforeSend:  function(jqXHR, settings) {
    jqXHR.setRequestHeader("Authorization", 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6=');
                    }

None of above works for me. What's the right syntax?

Thanks!!

xueru
  • 767
  • 2
  • 11
  • 21

2 Answers2

15

You can use for example loadBeforeSend event handler of the jqGrid defined as the following:

loadBeforeSend: function(jqXHR) {
    jqXHR.setRequestHeader("Authorization", 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6=');
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    I do see the request header being set properly in firebug by Oleg's solution. – xueru Apr 19 '11 at 03:35
  • 1
    Maybe I should put in another thread for this.. but I keep getting 401 error even the basic token is absolutely correct. If I don't set content-type in ajaxOptions, I would get 'anonymous user' at server side, after I set ajaxGridOptions: { contentType: 'application/json' }, I get 401 back. I am lost... – xueru Apr 19 '11 at 03:44
  • 1
    I did post [a new thread](http://stackoverflow.com/questions/5711433/ajax-configuration-issues-for-jqgrid) – xueru Apr 19 '11 at 04:01
  • 1
    I deleted the post where I describing the authorization problem. the issue is at the server side.. has nothing to do with jqgrid – xueru Apr 19 '11 at 16:14
  • 1
    @xueru: OK! I am glad to hear this. The solved problem is the good problem. – Oleg Apr 19 '11 at 16:36
1

Another option as of today is setting the header globally for all AJAX requests:

$.ajaxSetup({
    headers : {
        'Authorization' : 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6='
    }
});
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • This does not seem to work in my case. I add this global handler and it works for any $.ajax request but not for anything coming out of jqGrid. – Dan Feb 13 '17 at 16:22