一般来说一个jqueryajax只能调用一个一般处理程序(.ashx),但是有有些时候我们要更高效的使用一般处理程序,需要把一些方法比如增删改查写在一个ashx,方便统一管理。
前台JS:
function searchGuazhang() {
$.ajax({
url: "../AjaxHandle/GetGuazhang.ashx",
type: "post",
dataType: "json",
data: "action=SearchGuazhang&name=" + $("#txtguazhangname").val(),
success: function (data) {
$("#tbody").html(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{ alert(textStatus); }
});
}
ashx的ProcessRequest方法:
context.Response.ContentType = "text/plain";
string action = context.Request["action"].ToString();
System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(action);
if (methodInfo != null)
{
methodInfo.Invoke(this, new object[] {context});
}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">ashx的 </span><span style="font-family: Arial, Helvetica, sans-serif;">SearchGuazhang方法,该方法也是在执行页面回发请求时通过反射获取的方法</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="csharp">JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
int storeId = Convert.ToInt32(context.Session["storeId"]);
string name = context.Request.Params["name"];
DataTable table = incomeBll.SelectGuazhangBywhere(" c_name like '%"+name+"%' and i_sign=0 and storeId=" + storeId);
string str = "";
if (table.Rows.Count > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
str += "<tr class=\"tboot1\">" +
"<td>" + table.Rows[i]["c_acc"] + "</td>" +
"<td>" + table.Rows[i]["c_name"] + "</td>" +
//"<td>" + table.Rows[i]["fangjiaType"] + "</td>" +
"<td style=\"text-align: center\"><a href=\"#\" id='a" + i + "' οnclick='return guazhang(" + i +
")'>确定</a></td>" +
"</tr>";
}
}
else
{
str += "<tr class=\"tboot\">" +
"<td colspan='4'>该单位不存在!</td>";
}
String content = javaScriptSerializer.Serialize(str);
context.Response.Write(content);