Get rid of ashx and asmx and use jQuery. ajaxWebService to request WebMethod for concise processing of Ajax and ashxwebmethod
To develop ajax programs in WebForm, you need to use the general processing program (*. ashx) or web Service (*. asmx), and every ajax request must create such a file. As a result, if there are more ajax programs in a project, a bunch of files will be generated. ashx or. asmx, although the program itself does not affect, but the pile of files seems to have been hurt. Can I discard these. ashx and. asmx and choose a more concise method for ajax programs.
The answer is yes, that is, WebMethod. (This method is debugged in. net3.5 and has a problem in. net2.0)
First, create a public static method in the aspx. cs file, and add the WebMethod attribute.
[WebMethod]public static string GetUserName() {//......}
If you want to operate the session in this method, you have to set the EnableSession attribute of WebMethod to true. That is:
[WebMethod (EnableSession = true)] // or [WebMethod (true)] public static string GetUserName (){//......}
Then we will write an ajax program to access this program. Let's use jQuery.
$.ajax({type: "POST",contentType: "application/json",url: "WebForm2.aspx/GetUserName",data: "{}",dataType: "json",success: function(){.......}});
Here is a brief description of several parameters,
Type: Request type. post is required here. The WebMethod only accepts post requests.
ContentType: Content Encoding type when the message is sent to the server. We must use application/json here.
Url: the path of the server-side processing program of the request, in the format of "file name (including suffix)/method name"
Data: parameter list. Note that the parameters here must be strings in json format. Remember to use the string format, for example, "{aa: 11, bb: 22, cc: 33 ,...}". If
If you do not write a string, then jquery will serialize it into a string, so what the server receives is not in json format and cannot be blank, "{}" should be written even if no parameters are available, as shown in the preceding example.
This is why many people fail.
DataType: Data Type returned by the server. It must be json, and none of the others is valid. Because webservice returns data in json format, the format is {"d ":"......."}.
Success: the callback function after the request is successful. You can perform any processing on the returned data here.
We can see that some of the parameter values are fixed. Therefore, from the perspective of reusability, we can make an extension for jquery and make a simple encapsulation of the above function:
Create a script file named jquery. extend. js. Write a method named ajaxWebService in it (because webmethod is actually a WebService, the method is also valid for the request *. asmx). The Code is as follows:
/// <Summary> // jQuery prototype extension, re-encapsulate the Ajax request WebServeice /// </summary> /// <param name = "url" type = "String"> /// address for processing the request /// </ param> /// <param name = "dataMap" type = "String"> /// parameter, json string /// </param> /// <param name = "fnSuccess" type = "Function"> /// callback Function after successful request /// </param> $. ajaxWebService = function (url, dataMap, fnSuccess) {$. ajax ({type: "POST", contentType: "application/json", url: url, data: dataMap, dataType: "json", success: fnSuccess });}
Okay, so we can write the webmethod request as follows:
$.ajaxWebService("WebForm2.aspx/GetUserName", "{}", function(result) {......});
Finally, if you have a lot of ajax programs in your project (this situation may exist. I have done a website called sns, ajax for the whole process, and ajax is used for almost every operation ), if you think that the WebMethod method is scattered in various aspx pages, you can create a page (such as WebMethods. aspx.
The above section describes how to use jQuery to get rid of ashx and asmx. ajaxWebService requests WebMethod to process Ajax in a concise manner and hopes to help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!