在将客户端页面中的值传到handler处理类的时候,如果使用的请求方式为GET,处理参数只能是Request.QueryString["名称"]来获取;如果是使用的是Post,就必须使用Request.Form["名称"]来取值;不过不管使用哪种请求方式都可以使用Request["名称"],Request.Params["名称"]来获取值
例:
1、html页面
<!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>
<title></title>
<script type="text/javascript">
function btnClick() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var txt1Value = document.getElementById("txt1").value;
xmlhttp.open("POST", "HTMLPage2Handler.ashx", false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //x-www-form-urlencoded
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("txt2").value = xmlhttp.responseText;
}
}
}
xmlhttp.send("txt1=" + txt1Value);
}
</script>
<script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#Button1").click(function() {
var txt1Value = $("#Text1").val();
$.post("HTMLPage2Handler.ashx", {"txt1":txt1Value}, function(data, status) {
if (status == "success") {
$("#Text2").val(data);
}
});
});
});
</script>
</head>
<body>
<form id="form1">
<div>
<input type="text" id="txt1" />
<input type="text" id="txt2" />
<input type="button" id="btn1" value="传 值" οnclick="btnClick()"/>
</div>
<div>
<input type="text" id="Text1" />
<input type="text" id="Text2" />
<input type="button" id="Button1" value="Jquery传 值"/>
</div>
</form>
</body>
</html>
2、handler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MyWeb
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class HTMLPage2Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string txt1Value = context.Request.Form["txt1"];
context.Response.Write("Hello World "+ txt1Value );
}
public bool IsReusable
{
get
{
return false;
}
}
}
}