Cookies improve the way our website works, by using this website you are agreeing to our use of cookies. For more information see our privacy policy.
OK
Adds an HTTP header with the specified name and value to the request.
Request headers contain information about the sender, body of the request and required response.
First initialize an XMLHttpRequest object with the open method, then specify the necessary request headers with the setRequestHeader method and finally send the request with the send method.
Required. String that specifies the name of the request header to send.
The following list contains some of the commonly used names.
If you need a detailed list, please visit the HTTP request headers (Wikipedia) page.
Accept
The content types that are acceptable for the response. It is the responsibility of the server to consider this requirement.
Accept-Charset
The character sets that are acceptable for the response. It is the responsibility of the server to consider this requirement.
Content-Type
The content type of the request body (for POST requests).
Date
The current date and time when the request was sent.
value
Required. String that specifies the value of the HTTP request header.
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the setRequestHeader method:
Code
ajax.js
news.xml
<head><scripttype="text/javascript"src="ajax.js"></script><scripttype="text/javascript">var httpRequest = null;
function SendRequest () {
if (!httpRequest) {
httpRequest = CreateHTTPRequestObject (); // defined in ajax.js
}
if (httpRequest) {
// The requested file must be in the same domain that the page is served from.
var url = "news.xml";
httpRequest.open ("GET", url, true); // async
httpRequest.setRequestHeader ("Accept", "text/xml");
httpRequest.onreadystatechange = OnStateChange;
httpRequest.send (null);
}
}
function OnStateChange () {
if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js
alert ("Request complete.");
}
else {
alert ("Operation failed.");
}
}
}
</script></head><body><buttononclick="SendRequest ()">Send request</button></body>
function CreateHTTPRequestObject () {
// although IE supports the XMLHttpRequest object, but it does not work on local files.
var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
if (window.XMLHttpRequest && !forceActiveX) {
returnnewXMLHttpRequest();
}
else {
try {
returnnewActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
alert ("Your browser doesn't support XML handling!");
returnnull;
}
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
returnnewActiveXObject(progIDs[i]);
} catch(e) {};
}
}
returnnull;
}
function CreateXMLDocumentObject (rootName) {
if (!rootName) {
rootName = "";
}
var xmlDoc = CreateMSXMLDocumentObject ();
if (xmlDoc) {
if (rootName) {
var rootNode = xmlDoc.createElement (rootName);
xmlDoc.appendChild (rootNode);
}
}
else {
if (document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument ("", rootName, null);
}
}
return xmlDoc;
}
function ParseHTTPResponse (httpRequest) {
var xmlDoc = httpRequest.responseXML;
// if responseXML is not valid, try to create the XML document from the responseText property
if (!xmlDoc || !xmlDoc.documentElement) {
if (window.DOMParser) {
var parser = newDOMParser();
try {
xmlDoc = parser.parseFromString (httpRequest.responseText, "text/xml");
} catch (e) {
alert ("XML parsing error");
returnnull;
};
}
else {
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
returnnull;
}
xmlDoc.loadXML (httpRequest.responseText);
}
}
// if there was an error while parsing the XML document
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
}
if (errorMsg) {
alert (errorMsg);
returnnull;
}
// ok, the XML document is valid
return xmlDoc;
}
// returns whether the HTTP request was successful
function IsRequestSuccessful (httpRequest) {
// IE: sometimes 1223 instead of 204
var success = (httpRequest.status == 0 ||
(httpRequest.status >= 200 && httpRequest.status < 300) ||
httpRequest.status == 304 || httpRequest.status == 1223);
return success;
}
<?xmlversion="1.0"encoding="utf-8"?><news><channel><item><title>Paula Abdul leaves American Idol</title><description>Paula Abdul has officially confirmed that she won’t be returning to American Idol this season. "With sadness in my heart, I’ve decided not to return to Idol," she wrote on her Twitter page.</description><pubDate>Wed, August 5, 2009 19:52:31 +0200</pubDate><link>http://www.tribute.ca/news/index.php/its-official-paula-abdul-leaves-american-idol/2009/08/05/</link></item><item><title>Criminal Minds star Moore hit by car</title><description>Shemar Moore, 39, who plays FBI Agent Derek Morgan in the CBS drama Criminal Minds, was hit by a car while bicycling in Los Angeles last Saturday.</description><pubDate>Thu, August 4, 2009 13:28:12 +0200</pubDate><link>http://www.tribute.ca/news/index.php/criminal-minds-star-moore-hit-by-car/2009/08/04/</link></item><item><title>Madonna writes love song for ex-husband</title><description>Madonna has sparked speculation that she wants to get back with her ex-husband, Guy Ritchie, after writing a love song for him in which she calls him her "eternal love".</description><pubDate>Fri, July 31, 2009 04:01:50 +0200</pubDate><link>http://www.tribute.ca/news/index.php/madonna-writes-love-song-for-ex-husband/2009/07/31/</link></item></channel></news>