Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call a method in Web Service directly on the client side

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to call the method in Web Service directly on the client side". In the daily operation, I believe that many people have doubts about how to call the method in Web Service directly on the client side. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to call the method in Web Service directly on the client side". Next, please follow the editor to study!

In 1.Web.config, you need to configure the corresponding configuration items to run the ASP.NET AJAX framework, and of course, when you create an ASP.NET AJAX Enabled Web Site project, Web.config is already configured.

two。 If you want a WebService to be called by JS, you need to take a few steps:

◆ introduces the namespace with "using System.Web.Script.Services;" in the WebService file.

◆ adds a "[ScriptService]" attribute to this class.

◆ adds a "[WebMethod]" attribute to the method that needs to be called.

3. Client pages that call WebService should also be prepared accordingly. The first thing to do is to have a ScriptManager control in the page, and then you need to indicate the location of the WebService file in it. Such as:

< Services > < asp:ServiceReference Path= "~ / SayHelloService.asmx" / > < / Services >

Placing this code between the and tag allows the JS on this page to call methods in SayHelloService.asmx directly.

With the above three items ready, the JS code on this page can use the familiar "class name. method name" method to call the method in WebService directly.

The story behind magic

We know that although we always say "direct call", this means "direct call" at the grammatical level. In fact, it is impossible for JavaScript code to call the daemon directly, and the XMLHttpRequest object must be used in the middle. So, who on earth carried out the "cover-up" for us? The answer is in the following picture:

The picture above is the real secret of the so-called "direct call". As shown in the figure, now suppose you have a WebService class called Class1 with two methods: Method1 and Method2. When the WebService is processed by us using the method mentioned above, the ASP.NET AJAX framework automatically parses the class and generates a client proxy (Proxy) for this class on the client side. The proxy class is a JavaScript object with the same class name and method name as the background class. When we call from the client's JS script, we actually call this proxy class, while the proxy class uses the XMLHttpRequest object to invoke the background class in the traditional Ajax way. In this way, we feel as if we are calling something similar in the background directly in the foreground.

But. Where it still seems to be different.

From the above analysis, we know that ASP.NET AJAX calls background methods, in the final analysis, it is still the use of XMLHttpRequest objects, so the limitations of traditional Ajax make it impossible for us to be as convenient as calling methods in the background.

For example, we always call it in the background with "variable = class name. method name" or "variable = object name. method name", so the variable gets the return value of the method directly, but Ajax is not allowed, it must get the return value through the callback function, which is also reflected in the use of ASP.NET AJAX.

At this point, the study on "how to call methods in Web Service directly on the client side" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report