Go from original source jquery to access the Ashx file example
. ashx files for writing web handler . ashx files are similar to. aspx files, which can be used to invoke the HttpHandler class, which eliminates the control parsing of normal. aspx pages and the process of page processing. is actually a mixed file with HTML and C #.
The. ashx file is suitable for producing data formats that are processed by the browser that do not require postback processing, such as for generating dynamic pictures, dynamic text, and so on. Many need to use this kind of treatment. This document provides a simple demo to call the Ashx file and post the source code of the key file.
The following is the source code in the Login.ashx file in the demo:
Public classLogin:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Application/json"; //Get method to get the data passed//string username = context. request.querystring["username"]; //string Password = context. request.querystring["Password"]; //Post method to get the data passed stringUsername = context. request.form["username"]; stringPassword = context. request.form["Password"]; stringMessage =NULL; if(string. IsNullOrEmpty (username)) {message="user name cannot be empty"; Context. Response.Write ("{\ "success\": False,\ "message\": \ ""+ Message +"\"}");//This JSON format is very important, otherwise the error function of jquery will be executedcontext. Response.End (); } if(string. IsNullOrEmpty (password)) {message="The password cannot be empty"; Context. Response.Write ("{\ "success\": False,\ "message\": \ ""+ Message +"\"}"); Context. Response.End (); } if(!string. IsNullOrEmpty (username) &&!string. IsNullOrEmpty (password)) {if(username. ToUpper () = ="ADMIN"&& Password = ="123") {Message="Login Successful"; Context. Response.Write ("{\ "success\": True,\ "message\": \ ""+ Message +"\"}"); } Else{message="incorrect user name or password"; Context. Response.Write ("{\ "success\": False,\ "message\": \ ""+ Message +"\"}"); }} context. Response.End (); } Public BOOLisreusable {Get { return false; } } }View Code
The following is the source code in HTML:
<title>jsquery access ashx files </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 ("Execution failed"); } }); } </script> <div style="width:400px, height:300px, margin:0 Auto background: #c0c0c0;"> <dl style="width:270px;"> <dd><span> user name: </span><input style="width:150px;"Id="txtUserName"Type="text"></dd> <dd><span> Password: </span><input style="width:150px;"Id="Txtpassword"Type="Password"></dd> <dd><input style="width:65px; height:23px; float:right ;"onclick="Login ()"Value="Login"Type="Button"></dd> </dl> </div>View Code
jquery Access Ashx File example