jquery访问ashx文件示例代码 - Web前端
作者:98wpeu发布时间:2026-08-02分类:网页前端技术浏览:1
导读:.ashx文件用于写WEBhandler的。.ashx文件与.aspx文件类似,可以通过它来调用httpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程...
.ashx 文件用于写WEB handler的。.ashx文件与.aspx文件类似,可以通过它来调用httpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程。其实就是带html和c#的混合文件。
.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中的源码:
<!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>JSquery访问ashx文件</title>
<script language="javascript" type="text/JavaScript" src="Scripts/jQuery-1.4.1.min.js"></script>
<script language="Javascript" type="text/javascript">
function login() {
$.Ajax({
url: 'common/handler/Login.ashx',
type: 'POST',
data: { 'username': $("#txtUsername").val(), 'password': $("#txtPassword").val() },
dataType: 'json',
timeout: 50000,
//contentType: 'application/json;charset=utf-8',
success: function (response) {
alert(response.message);
},
error: function (err) {
alert("执行失败");
}
});
}
</script>
</head>
<body>
<div style="width:400px; height:300px; margin:0 auto; background:#c0c0c0;">
<dl style=" width:270px;">
<dd><span>用户名:</span><input type="text" style=" width:150px;" id="txtUsername" /></dd>
<dd><span>密 码:</span><input type="password" style=" width:150px;" id="txtPassword" /></dd>
<dd><input type="button" style=" width:65px; height:23px; float:right;" onclick="login()" value="登录" /></dd>
</dl>
</div>
</body>
</html>
相关推荐
- JQuery Tips相关(1)----关于$.Ready() - Web前端
- 使用jquery.validate自定义方法实现"手机号码或者固话至少填写一个"的逻辑验证 - Web前端
- JQuery 给元素绑定click事件多次执行的解决方法 - Web前端
- jQuery实现倒计时按钮功能代码分享 - Web前端
- jQuery实现购物车多物品数量的加减+总价计算 - Web前端
- jquery修改网页背景颜色通过css方法实现 - Web前端
- jquery实现保存已选用户 - Web前端
- jQuery实现简单网页遮罩层/弹出层效果兼容IE6、IE7 - Web前端
- jQuery filter函数使用方法 - Web前端
- jquery中常用的函数和属性详细解析 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3【第六章】Foundation之按钮和下拉功能 - Web前端
- 4jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 9在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


