

.ashx文件适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。很多需要用到此种处理方式。此文档提供一个简单的调用ashx文件的Demo,并贴出关键文件的源码。
以下为Demo中Login.ashx文件中的源码:
public class Login : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
//GET方式获取传递的数据
//string username = context.Request.QueryString["username"];
//string password = context.Request.QueryString["password"];
//POST方式获取传递的数据
string username = context.Request.Form["username"];
string password = context.Request.Form["password"];
string message = null;
if (string.IsNullOrEmpty(username))
{
message = "用户名不能为空";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");//此JSON格式非常重要,否则会执行jquery的的error函数
context.Response.End();
}
if (string.IsNullOrEmpty(password))
{
message = "密码不能为空";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");
context.Response.End();
}
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
if (username.ToUpper() == "ADMIN" && password == "123")
{
message = "登录成功";
context.Response.Write("{\"success\":true,\"message\":\"" + message + "\"}");
}
else
{
message = "用户名或密码错误";
context.Response.Write("{\"success\":false,\"message\":\"" + message + "\"}");
}
}
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}以下为html中的源码:
jsquery访问ashx文件 script>
