62,074
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Sending Request Data Using GET and POST</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name=ProgId content=VisualStudio.HTML>
<meta name=Originator content="Microsoft Visual Studio .NET 7.1">
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(windo.HTMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function createQueryString()
{
var firstName = document.getElementById("firstName").value;
var middleName = document.getElementById("middleName").value;
var birthday = document.getElementById("birthday").value;
var queryString = "firstname=" + firstName + "&middlename=" + middleName + "&birthday=" + birthday;
return queryString;
}
function doRequestUsingGET()
{
createXMLHttpRequest();
var queryString = "WebForm3.aspx?";
queryString = queryString + createQueryString() + "×tamp=" + new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET",queryString, true);
xmlHttp.send(null);
}
function doRequestUsingPOST()
{
createXMLHttpRequest();
var url = "WebForm3.aspx?timeStamp=" + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(queryString);
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
parseResult();
}
}
}
function parseResult()
{
var responseDiv = document.getElementById("serverResponse");
if(responseDiv.hasChildNodes())
{
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}
</script>
</head>
<body>
<h1>Enter your first name, middle name, and birthday:</h1>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" id="firstName"></td>
</tr>
<tr>
<td>Middle Name:</td>
<td><input type="text" id="middleName"></td>
</tr>
<tr>
<td>Birthday:</td>
<td><input type="text" id="birthday"></td>
</tr>
</table>
<form action="#">
<input type="button" value="Send parameters using GET" onclick="doRequestUsingGET();">
<br><br>
<input type="button" value="Send parameters using POST" onclick="doRequestUsingPOST();">
<br>
<h2>Server Response:</h2>
<div id="serverResponse"></div>
</form>
</body>
</html>
private void Page_Load(object sender, System.EventArgs e)
{
Response.ContentType = "text/xml";
string firstName = Request.QueryString["firstname"] != "" ? Request.QueryString["firstname"].ToString() : "";
string middleName = Request.QueryString["middlename"] != "" ? Request.QueryString["middlename"].ToString() : "";
string birthday = Request.QueryString["birthday"] != "" ? Request.QueryString["birthday"].ToString() : "";
string returnValue = "FirstName:" + firstName + ",MiddleName:" + middleName + ",Birthday:"+birthday;
Response.Write(returnValue);
Response.Flush();
Response.Close();
}