How to set request header to the ajax object for jqGrid
Asked Answered
T

2

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

Thevenot answered 18/4, 2011 at 17:29 Comment(0)
Y
15

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

loadBeforeSend: function(jqXHR) {
    jqXHR.setRequestHeader("Authorization", 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6=');
}
Yasmin answered 18/4, 2011 at 17:40 Comment(5)
I do see the request header being set properly in firebug by Oleg's solution.Thevenot
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...Thevenot
I did post a new threadThevenot
I deleted the post where I describing the authorization problem. the issue is at the server side.. has nothing to do with jqgridThevenot
@xueru: OK! I am glad to hear this. The solved problem is the good problem.Yasmin
C
1

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

$.ajaxSetup({
    headers : {
        'Authorization' : 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6='
    }
});
Cherin answered 1/8, 2016 at 16:31 Comment(1)
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.Zwieback

© 2022 - 2024 — McMap. All rights reserved.