ajax - setrequestheader() method.

the ajax - setrequestheader() method adds custom http headers to the request.

note :- the empty string is legal and represents the empty header value.

AJAX - setRequestHeader Method syntax

                   var  returnvalue  =   object.setrequestheader(header , value);
parameterdescriptiontype
headerspecifies the header name.string
valuespecifies the header value.string

ajax - setrequestheader method example: the following script sets the http content-type header to "text/html" before sending the request body.

           <script>
               
               var   xmlhttp = new xmlhttprequest();                       
                         
                       xmlhttp.open("get",".//login.jsp",false);
                        
                       xmlhttp.setrequestheader("content-type","text/html");
                        
                       xmlhttp.sent(requestbody);
                        
            </script>

head part

                http/1.1   200   ok
                
                server : microsoft-iis/4.0
                
                cache-control: max-age=212200 
                
                expires: tue , 12 apr 2012 11:34:01 gmt
                
                date: mon, 05 may 2012 12:1:02 gmt
                
                content-type: text/html
                
                accept-ranges: bytes
                
                last-modified: thu, 15 mar 2012 12:03:20 gmt
                
                etag: "0a7ccac50cbc11:1aad"
                
                content-length: 65332 
             

ajax - getresponseheader() method

the ajax - getresponseheader() method returns the specified response header.

the AJAX - getResponseHeader Method syntax

              var return_value  =   object.getresponseheader("header");
parameterdescriptiontype
headerspecifies the response header name.string
return_valuestring that receives the response header value.string

ajax - getresponseheader method example: using head requests, to find the last-modified of another file.

           <script>
               
               var   xmlhttp  =  new xmlhttprequest();                       
                         
                       xmlhttp.open("head",".//login.html",true);
                      
                      
                       xmlhttp.send(); 
                      
                      
                       xmlhttp.onreadystatechange  =  function() {
                       
                      if (xmlhttp.readystate  == 4)  {
                            
alert ("file was last-modified-"+ xmlhttp.getresponseheader("last-modified"));
                              
                          }
                          
                    }      
                               
            </script>

ajax - getallresponseheader() method

the ajax - getallresponseheader() method returns the complete list of response headers.

this method has no arguments. note :- each name/value pair is delimited by a carriage return/line feed (cr/lf) sequence.

ajax - getallresponseheader() method syntax:

              var return_value  =   object.getallresponseheader();

ajax - getallresponseheader() method example

           <script>
               
               var   xmlhttp  =  new xmlhttprequest();                       
                         
                       xmlhttp.open("get",".//demofile.txt", true);
                      
                       xmlhttp.send(); 
                      
                       xmlhttp.onreadystatechange  =  function() {
                       
                      if (xmlhttp.readystate  == 4)  {
                            
                                       
                                       alert (xmlhttp.getallresponseheader());
                              
                          }  }
           </script>