>  기사  >  웹 프론트엔드  >  jquery 액세스 ashx 파일 샘플 code_jquery

jquery 액세스 ashx 파일 샘플 code_jquery

WBOY
WBOY원래의
2016-05-16 16:40:061364검색

.ashx 파일은 웹 핸들러를 작성하는 데 사용됩니다. .ashx 파일은 .aspx 파일과 유사하며 이를 사용하여 일반 .aspx 페이지의 컨트롤 구문 분석 및 페이지 처리를 제거하는 HttpHandler 클래스를 호출할 수 있습니다. 사실 HTML과 C#이 혼합된 파일입니다.

.ashx 파일은 동적 사진, 동적 텍스트 및 기타 콘텐츠 생성과 같이 포스트백 처리가 필요하지 않은 브라우저 처리를 위한 데이터 형식을 생성하는 데 적합합니다. 많은 사람들이 이 처리 방법을 사용해야 합니다. 이 문서는 ashx 파일 호출에 대한 간단한 데모를 제공하고 주요 파일의 소스 코드를 게시합니다.

다음은 데모의 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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.