Dump ASHX and ASMX use Jquery.ajaxwebservice request WebMethod concise processing Ajax_ajax related

Source: Internet
Author: User
Tags extend string format

Developing AJAX programs under WebForm requires the help of a generic handler (*.ASHX) or Web service (*.asmx), and each AJAX request builds one such file, so if there are more AJAX programs in a project, It's bound to produce a bunch of. ashx or. asmx, although not in the process itself, but that pile of documents seems to feel a lot of harm. So could you throw away these. ashx and. asmx, and choose a more concise way to do AJAX programs.

The answer is yes, that is: WebMethod. (This method is debugged under the. net3.5 version, with a problem under. net2.0)

First, create a public static method in the Aspx.cs file, and then add the WebMethod attribute.

[WebMethod]
public static string GetUserName () 
{
//...

If you want to manipulate the session in this way, you have to set the WebMethod EnableSession property to True. That

[WebMethod (EnableSession = True)]//or [WebMethod (true)] public
static string GetUserName () 
{
//.....
}

Then we'll write Ajax programs to access this program, and we'll use jquery.

$.ajax ({
type: "POST",
contentType: "Application/json",
URL: "Webform2.aspx/getusername"
, Data: "{}",
DataType: "JSON",
success:function () {...}

Here are a few parameters to do a simple description,

Type: the kind of request, which must be post. The WebMethod method accepts only post type requests.

ContentType: Content encoding type when sending information to the server. We must use Application/json here.

URL: The path of the requested server-side handler, in the form "filename (with suffix)/method name"

Data: Parameter list. Note that the parameter here must be a JSON-formatted string, remembering to be a string format, such as: "{aa:11,bb:22,cc:33, ...}". If

What you write is not a string, that jquery will actually serialize it into a string, then the server side is not JSON format, and cannot be empty, even if there is no parameter to write "{}", as the example above.

A lot of people don't succeed, that's why.

DataType: The data type returned by the server. Must be JSON and none of the others are valid. Because WebService is a JSON format that returns data in the form of: {"D": "..."}.

Success: callback function after a successful request. You can do any processing of the returned data here.

We can see that some of the parameter values are fixed, so from the point of view of reusability, we can extend the jquery to a simple encapsulation of the above function:

We build a script file called Jquery.extend.js. In the inside write a method called Ajaxwebservice (because WebMethod is actually webservice, so the method is valid for the request *.asmx), the code is as follows:

<summary>
///jquery prototype extensions, encapsulating AJAX requests Webserveice
///</summary>
///<param name= "URLs" Type= "string" >
///addresses the requested address
///</param>
///<param name= "Datamap" type= "string" >
/// Parameters, JSON-formatted 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
});

OK, so we ask the WebMethod method to write this:

 
 

Finally: If you have a lot of Ajax programs in your project (this is possible, I have done a website, is a SNS, full Ajax, almost every operation with Ajax, you feel that the WebMethod method written in each ASPX page is very fragmented, then you can specifically build a page ( such as: webmethods.aspx) to store.

The above is a small set to introduce the dump ASHX and ASMX use Jquery.ajaxwebservice request WebMethod Concise processing Ajax, hope for everyone to help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.