Home >Web Front-end >JS Tutorial >Various ways to implement JS calling background methods for data interaction_javascript skills

Various ways to implement JS calling background methods for data interaction_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:24:501491browse

During the project development process, data interaction between the front end and the backend is required in many places. Several typical and commonly used methods include using the AutopostBack property of the control, Button submission form, etc. But these are conditional. AutoPostBack is real-time but will refresh the page. Button submission form cannot achieve real-time data interaction. Of course, when it comes to the data interaction between the foreground and the background, ajax cannot be missed. Ajax realizes asynchronous interaction between the foreground and the background data and ensures real-time and partial refresh. However, some data do not require asynchronous interaction. For example, when the interactive data is a condition for the next step of execution, the program must wait until the interaction between the foreground data and the background data is completed before the program can continue to execute. Therefore, it is still necessary to master the method of interaction between js and background data.

Method 1

Background method:

Copy code Code As follows:

// Needs to be identified as WebMethod
[System.Web.Services.WebMethod]
< ;SPAN style="COLOR: #ff0000">// Note that the method to be called by the front desk must be public and static
public static string Say(string name)
{
string result = "Hello:" name;
return result;
}

Front-end js:
Copy code The code is as follows:





Method 2

Background method:
Copy code The code is as follows:

protected string Say(string strCC)
{
strCC = "Hello! " strCC;
return strCC;
}

Front-end js:
Copy code The code is as follows:

function Show()
{
var v = "China";
var s = '<%=Say("' v '") %>'; // Hello! " V "
alert(s);
}



Method 3

Background method:
Copy code The code is as follows:

// Need to be identified as WebMethod
[System.Web.Services.WebMethod]
//
Note that the method to be called by the front desk must be public and static
public static string Say(string name)
{
string result = "Hello:" name;
return result;
}


Front-end js:
Copy code The code is as follows:



//WebSerCustomer.asmx The page name of the background webservice class




Summary
For methods one and three, identifying System.web.Services.webmethod can declare that a method can be called through the client js function, and the background method must be declared as public and static, precisely because the method must be declared as static , making both methods have limitations, that is, only static member variables are allowed to be accessed in static methods. So if you want to call background methods in these two ways, non-static member variables cannot be accessed in the background method.

For method two, although there are no restrictions on the background method, when the frontend is called, since <%=%> is read-only, the parameters passed by the frontend to the backend do not actually exist. That is, it cannot be obtained from the background. Therefore, method 2 is suitable for calling background methods to be processed and returned to the client for use, but is not suitable for transmitting data to the background for background use.
Statement:
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