Specifies the name of an HTTP header.
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue)
If another header already exists with this name, it is replaced.
The following sample demonstrates usage of the setRequestHeader method
The following script example posts a DOMDocument to an .ASP page on a server and returns the result as a new XML document.
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0" />
</SCRIPT>
</HEAD>
<BODY LANGUAGE=javascript onload="return onload()">
<TABLE border='2' align='center'>
<TR><TD width="150" align='center'>
<LABEL>Name</LABEL>
</TD><TD>
<input name='Customername' type="edit"/>
</TD></TR>
<TR><TD width="150" align='center'>
<LABEL>Telephone number</LABEL>
</TD><TD>
<input type="edit" name='telno'/>
</TD></TR>
</TABLE>
<TABLE align='center'>
<TR><TD width="150" align='center'>
<input type='button' value='send Information' align='center' onclick='sendinfo()'/>
</TD></TR>
</TABLE>
<script language="vbscript">
function onload()
Set pi = mydata.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
mydata.insertBefore pi, mydata.firstChild
end function
function sendinfo()
Dim myhttp
' Do validation of input data before sending it.
if(not(Customername.value = "")) then
mydata.documentElement.getElementsByTagName("Name").item(0).text =Customername.value
mydata.documentElement.getElementsByTagName("TelNo").item(0).text =telno.value
Set myhttp=CreateObject("Msxml2.XMLHTTP")
myhttp.open "GET", "http://SamplesServer/httpreqserver.asp", false
' Simulate message sent by a custom user agent.
myhttp.setRequestHeader "User-Agent", "MyCustomUser"
mydata.async=false
myhttp.send mydata.XMLDocument
alert myhttp.responseText
else
alert "invalid data"
end if
end function
</script>
</BODY>
</HTML>
<XML id="mydata">
<MyStruct>
<Name/>
<TelNo/>
</MyStruct>
</XML>
http://SamplesServer/httpreqserver.asp
<%@ language=javascript %>
<%
Response.Expires = -1000;
// Load the posted XML document.
var doc = Server.CreateObject("Msxml2.DOMDocument");
doc.async=false;
doc.load(Request);
var result = Server.CreateObject("Msxml2.DOMDocument");
// Now process the order and build the result document.
var userAgent = Request.ServerVariables("HTTP_User_Agent");
var OutputString="Data for "+ doc.documentElement.childNodes.item(0).text+" Added";
Response.ContentType = "text/xml";
if(userAgent == "MyCustomUser")
{
result.loadXML("<result>" + OutputString +" </result>");
var pi = result.createProcessingInstruction("xml", "version='1.0'");
result.insertBefore( pi, result.firstChild);
result.save(Response);
}
else
{
Response.Write("<P><B>" + OutputString+" </B></P>");
}
%>
Output
<?xml version="1.0"?> <result>Data for [input name] Added</result>
Applies To: XMLHttpRequest Object