<script language="javascript" type="text/javascript">
function RequestByPost(value) {
var xmlhttpps;
try {
xmlhttpps = new ActiveXObject("Msxml.XMLHTTP");
} catch (a) {
try {
xmlhttpps = new ActiveXObject("Microsoft.XMLHTTP");
} catch (a) {
xmlhttpps = false;
}
}
if (!xmlhttpps && typeof XmlHttpRequest != 'undefined') {
xmlhttpps = new XMLHttpRequest();
}
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<GetTime xmlns="http://tempuri.org/">';
data = data + '<input>' + value + '</input>';
data = data + '</GetTime>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
//var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// xmlhttp = createXMLHttp();
// xmlHttpps = createXMLHttp;
alert(xmlhttpps);
var URL = "http://localhost/WebService/CityService.asmx";
xmlhttpps.open("POST", URL, false);
xmlhttpps.onreadystatechange = function () {
if (xmlhttpps.readyState == 4) {
if (xmlhttpps.status == 200) {
document.getElementById("TextArea1").value = xmlhttpps.responseText;
// alert(xmlhttpps.responseText);
}
}
};
xmlhttpps.SetRequestHeader("Content-Type", "text/xml; charset=gb2312");
xmlhttpps.SetRequestHeader("SOAPAction", "http://tempuri.org/GetTime");
xmlhttpps.send(data);
}
</Script>
<body>
<textarea id="TextArea1" name="S1" rows="10"></textarea>
<input type="button" value="CallWebserviceByPost" onclick="RequestByPost('hello')" />
</body>
------------webservice.cs----------------------------
[WebMethod]
public XmlNode GetTime(string input)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<envelope><date_time>" + input + " " + DateTime.Now.ToLongDateString() +
DateTime.Now.ToLongTimeString() + "</date_time></envelope>");
return doc.DocumentElement;
}
在IE下可以显示
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetTimeResponse xmlns="http://tempuri.org/"><GetTimeResult><envelope xmlns=""><date_time>hello 2011年6月25日 星期六16:41:27</date_time></envelope></GetTimeResult></GetTimeResponse></soap:Body></soap:Envelope>