Get rid of ashx/asmx and use jquery. ajaxwebservice to request webmethod. Ajax processing is more concise.

Source: Internet
Author: User

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 you do not write a string, jquery will actually 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) to store it.
 

Source code download

Address: http://www.cnblogs.com/xumingxiang/archive/2010/05/04/1727609.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.