Home > Web Front-end > JS Tutorial > body text

jQuery.ajaxWebService requests WebMethod to handle Ajax

php中世界最好的语言
Release: 2018-04-03 17:38:38
Original
1640 people have browsed it

This time I will bring you jQuery.ajaxWebService to request WebMethod to process Ajax, and jQuery.ajaxWebService to request WebMethod to process Ajax. What are the precautions?The following is a practical case, let's take a look.

To develop ajax programs under WebForm, you need to use general processing programs (*.ashx) or web services (*.asmx), and each ajax request must create such a file. In this way , if there are too many ajax programs in a project, a bunch of .ashx or .asmx will inevitably be generated. Although it is not harmful to the program itself, the bunch of files always looks harmful. So can we throw away these .ashx and .asmx and choose a more concise way to do the ajax program?

The answer is yes, that is: WebMethod. (This method was debugged under .net3.5 version and has problems 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() 
{
//......
}
Copy after login

If you want to operate the session in this method, you must set the EnableSession attribute of WebMethod to true. That is:

[WebMethod(EnableSession = true)]//或[WebMethod(true)]
public static string GetUserName() 
{
//......
}
Copy after login

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(){.......}
});
Copy after login

Here is a brief explanation of several parameters,

type: the type of request, post must be used here. The WebMethod method only accepts post type requests.

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

url: The path to the requested server-side handler, in the format of "file name (including suffix)/method name"

data: parameter list. Note that the parameters here must be string in json format, remember to be in string format, such as: "{aa:11,bb:22,cc:33, ...}". If

what you write is not a string, then jquery will serialize it into a string, then what is received on the server side is not in json format, and cannot be empty. Even if there are no parameters, it must be written as " {}", as in the above example.

Many people fail, and this is the reason.

DataType: Data type returned by the server. It must be json, anything else is invalid. Because the webservice returns data in json format, its form is: {"d":"...."}.

Success: Callback function after the request is successful. You can do whatever you want with the returned data here.

We can see that some of the parameter values ​​​​are fixed, so from the perspective of reusability, we can make an extension for jquery and make a simple encapsulation of the above function:

We create a script file called jquery.extend.js. Write a method called ajaxWebService inside (because webmethod is actually WebService, so this method is also valid for requesting *.asmx). The code is as follows:

/// <summary>
/// jQuery原型扩展,重新封装Ajax请求WebServeice
/// </summary>
/// <param name="url" type="String">
/// 处理请求的地址
///</param>
/// <param name="dataMap" type="String">
/// 参数,json格式的字符串
///</param>
/// <param name="fnSuccess" type="Function">
/// 请求成功后的回调函数
///</param>
$.ajaxWebService = function(url, dataMap, fnSuccess) {
$.ajax({
type: "POST",
contentType: "application/json",
url: url,
data: dataMap,
dataType: "json",
success: fnSuccess
});
}
Copy after login

Okay, so we can write the webmethod method like this :

$.ajaxWebService("WebForm2.aspx/GetUserName", "{}", function(result) {......});
Copy after login

Finally: If there are a lot of ajax programs in your project (this situation is possible, I have made a website, it is an SNS, the whole process is ajax, almost every operation uses ajax) , you feel that writing WebMethod methods in various aspx pages is very fragmented, then you can create a special page (such as: WebMethods.aspx) to store them.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to set up an Ajax request to open a new window immediately after a successful request

Ajax request response to open a new window is blocked How to handle interception

The above is the detailed content of jQuery.ajaxWebService requests WebMethod to handle Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template