發送XMLhttp請求
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PIX_Test.ASPx.cs" Inherits="ZK_PIX.PIX_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<Html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>無標題頁</title>
<script type="text/Javascript" language="Javascript">
var XMLHttpReq = false;
var uname;
var psw;
//創建XMLHttpRequest對象
function createXMLHttpRequest() {
if (window.XMLHttpRequest) { //Mozilla 浏覽器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) { // IE浏覽器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
}
//發送請求函數
function sendRequest() {
createXMLHttpRequest();
var XML = createXML();
window.alert(XML);
XMLHttpReq.open("POST", "/PIX_RegistPatIEnt.ASPx", false);
XMLHttpReq.onreadystatechange = processResponse; //指定響應函數
XMLHttpReq.setRequestHeader("Content-Type", "application/x-www-form- urlencoded");
XMLHttpReq.send(XML); // 發送請求
}
// 處理返回信息函數
function processResponse() {
if (XMLHttpReq.readyState == 4) { // 判斷對象狀態
if (XMLHttpReq.status == 200) { // 信息已經成功返回,開始處理信息
var res = XMLHttpReq.responseText;
window.alert(res);
} else { //頁面不正常
window.alert("您所請求的頁面有異常。");
}
}
}
//創建XML
function createXML() {
var XML = "<?XML version=\"1.0\" encoding=\"gbk\" ?><PatIEnt><domain code=\"010101\" LPID=\"12345\" Flag=\"0\" /><name>測試</name><gender>男</gender><birth>19730303</birth><blood>B</blood><birthplace>0405</birthplace><code name=\"軍官證\" type=\"0\"/><addr /><hp /><wp /><email /><mobile /></PatIEnt>";
return XML;
}
</script>
</head>
<body onload="sendRequest()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</Html>
處理請求的XML
protected void Page_Load(object sender, EventArgs e)
{
CheckPage.SetHeader();
StreamReader reader = new StreamReader(Request.InputStream);
String XML = reader.ReadToEnd();
}