[Asp.net] An instance that uses Ajax and Jquery to transmit parameters to the background and return values. ajaxjquery
I. Preface
In the past, when I was working on a project, I encountered asp.net's front and back-end data transmission through AJAX. At that time, I had not summarized it myself. Although it was easy, I still had to summarize it for future study, think.
II. Key Points Analysis
The front-end of asp.net can enter the background by pressing F7, but you must add [WebMethod] to capture parameters passed by AJAX in the background through AJAX. However, AJAX can be captured in asp.net using a lot of "general processing programs. The specific operations are described below.
Iii. Usage plan
Here, we provide two solutions for data transmission:
Solution 1: directly call the backend
Front-end:
<% -- Introduce JQuery -- %> <script src = "js/jquery-2.1.1.min.js"> </script> <% -- pass parameters to the background using AJAX, call backend parameters -- %> <script type = "text/javascript" >$ (function () {<% -- triggered when txtUserName loses focus -- %> $ ('# txtusername '). blur (function () {var username = $ (this ). val (); $. ajax ({type: "post", contentType: "application/json", // url: "index. aspx/GetValueAjax ", // WebAjaxForMe. aspx is the target file, and GetValueAjax is the method data in the target file: "{username: '" + username + "'}", // username is the parameter to be passed in the background (the parameter here is optional) success: function (result) {alert (result. d); // result. d is the parameter returned by the background }})}) </script> <input id = "txtUserName" type = "text"/>
Background: Be sure to add [WebMethod]
[WebMethod] // you must add [WebMethod] public static string GetValueAjax (string username) to the front of the method) // This method requires a static method. The keyword static is used. {// here, you can perform any operation on the passed parameters. return username ;}
Solution 2 General processing program
After creating the general processing program ajaxtest. ashx, complete the following operations:
Front-end:
<% -- Introduce JQuery -- %> <script src = "js/jquery-2.1.1.min.js"> </script> <% -- use ajax to pass Parameters to a general handler, call a function -- %> <script type = "text/javascript" >$ (function () {<% -- triggered when txtYiBan loses focus -- %> $ ('# txtYiBan '). blur (function () {var username = $ (this ). val (); $. ajax ({type: "GET", url: "ajaxtest. ashx? Json = "+ username, // ajaxtest. ashx is the target file dataType: "text", success: function (result) {alert (result. d); // result. d is the parameter returned by the background }})}) </script> <input id = "txtYiBan" type = "text"/>
Ajaxtest. ashx General handler:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. services; using System. web. sessionState; namespace aspAjaxTest {// <summary> // ajaxtest abstract description /// </summary> [WebService (Namespace =" https://tempuri.org/ ")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] public class ajaxtest: IHttpHandler {HttpContext Context; // <summary> // obtain the passed value, call other methods /// </summary> /// <cke: param name = "context"> </cke: param> public void ProcessRequest (HttpContext context) {Context = context; context. response. clear (); context. response. contentType = "text/html; charset = UTF-8"; // obtain the passed value string methodName = GetQueryString ("json "); // you can call other methods ------ see the following} // <summary> // obtain the passed value /// </summary> /// <cke: param name = "name"> </cke: param> // <returns> </returns> string GetQueryString (string name) {// get the passed value return Context. request. params [name];} public bool IsReusable {get {return false ;}}}}
Methods that can be called:
Private string GetJsonStr (string methodName, string session, string strname, string Userid, string strWhere) {string jsonString = ""; switch (methodName) {case "GetWorksContent ": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. addHistory (). getRMContent (session); // jsonString = "1"; break; case "GetWorkssetContent": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. worksset (). getRMContent (session, strname, Userid); break; case "GetWorkContent": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. works (). getRMContent (session, strWhere); break; case "GetWorkssetByTimeContent": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. workssetByTime (). getRMContent (session, strname, Userid); break; case "GetWorkssetByPositionContent": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. workssetByPosition (). getRMContent (session, strname, Userid); break; case "GetWorkssetByArtistidContent": // call the paging effect jsonString = new JCZB. YMGJ. web. web. webManager. home. workssetByArtistid (). getRMContent (session, strname, Userid); break;} return jsonString ;}
4. What to learn in the aspx background can be captured using the following methods:
workstateValue = Request.QueryString["workstateValue"];
It can be used in general processing programs:
return Context.Request.Params[name];