Home  >  Article  >  Web Front-end  >  jquery access ashx file sample code_jquery

jquery access ashx file sample code_jquery

WBOY
WBOYOriginal
2016-05-16 16:40:061414browse

.ashx file is used to write web handler. The .ashx file is similar to the .aspx file. You can use it to call the HttpHandler class, which eliminates the control parsing and page processing of ordinary .aspx pages. In fact, it is a mixed file with HTML and C#.

.ashx files are suitable for generating data formats for browser processing that do not require postback processing, such as for generating dynamic pictures, dynamic text and other content. Many need to use this processing method. This document provides a simple demo of calling ashx files and posts the source code of key files.

The following is the source code in the Login.ashx file in the Demo:

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; 
} 
} 
}

The following is the source code in 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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn