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

Source: Internet
Author: User
General processing is required. 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 inevitably 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.
First, create a public static method in the Aspx. CS file, and add the webmethod attribute.
For example: [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 StringGetUserName ()
{
 //......
}

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 we will briefly describe 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 expand jquery and encapsulate the above functions:
Create a script file named jquery. Extend. js. Write a method named ajaxwebservice in it (because webmethod is actually a WebService, this method is also valid for the request *. asmx ),CodeAs follows:

// /


// /Jquery prototype extension, re-encapsulate Ajax request webserveice
// /
// /
// /Address for processing the request
// /
// /
// /Parameter, a string in JSON format
// /
// /
// /Callback function after successful request
// /
$. 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 your project has many Ajax programs (this situation may exist. I have done a website called SNS and Ajax is used throughout the process. 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.

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.