How to set request header to the ajax object for jqGrid

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!!


Solution 1:

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

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

Solution 2:

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

$.ajaxSetup({
    headers : {
        'Authorization' : 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6='
    }
});