Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set request header to the ajax object for jqGrid

Tags:

ajax

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

like image 537
xueru Avatar asked Apr 18 '11 17:04

xueru


People also ask

How to add request headers javascript?

The only way to add headers to a request from inside a browser is use the XmlHttpRequest setRequestHeader method. Using this with "GET" request will download the resource. The trick then is to access the resource in the intended way.

What is header in ajax?

The headers are additional key-value pairs send along with ajax request using the XMLHttpRequest object. An asynchronous HTTP request to the server by using The ajax() function and by including the header it describes to the server what kind of response it accept.

How do I add a request header in HTML?

Fill out the Create a header fields as follows: In the Name field, enter the name of your header rule (for example, My header ). From the Type menu, select Request, and from the Action menu, select Set. In the Destination field, enter the name of the header affected by the selected action.


1 Answers

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

loadBeforeSend: function(jqXHR) {
    jqXHR.setRequestHeader("Authorization", 'Basic YWRtaW5AZGVmYXVsdC5jb206YWRTwa6=');
}
like image 118
Oleg Avatar answered Dec 31 '22 15:12

Oleg