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

What's the use of ASP.NET AJAX?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the use of ASP.NET AJAX". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of ASP.NET AJAX"?

In general, there are successes and failures, and the same is true for asynchronous calls, where request exceptions occur. In traditional Ajax, the callback function is called regardless of whether the request succeeds or fails, and then we can judge the status of the XMLHttpRequest object to determine whether the request was successful or not, and if it fails, we can handle the exception.

However, in ASP.NET AJAX applications, callback functions are called only if they are successful, so what if they fail? In fact, ASP.NET AJAX allows us to specify two callback functions when calling the background method. The syntax is as follows:

Class name. Method name (parameter 1, parameter 2, …... , parameter n, callback function if the request succeeds, callback function if the request fails)

Only in the previous application, we omitted the second callback function. To understand this place, let's do a small experiment, open the ASPNETAJAXTest program in the previous article, and modify the contents of the ajax.js file as follows:

Ajax.js:

/ / JS function called when btnSayHello is clicked function btnSayHello_onClick () {SayHelloService.SayHello (OnSucceeded,OnFailded);} / callback function function OnSucceeded (reusltText) {$get ("result") [xss_clean] = reusltText;} / / callback function function OnFailded (error) {$get ("result") [xss_clean] = "failed. Error message:" + error.get_message ();}

The callback function in case of an exception has a parameter error, which is an object defined by an ASP.NET AJAX framework and contains exception information. Where the get_message () method returns the exception information string.

To see the effect, we also need to do some damage in WebService to cause an exception in the request. The WebService after "destruction" is as follows:

SayHelloService.cs: using System; using System.Web;using System.Collections; using System.Web.Services; using System.Web.Services.Protocols;using System.Web.Script.Services; [WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class SayHelloService System.Web.Services.WebService {public SayHelloService () {} [WebMethod] public string SayHello () {/ / Hello myHello = new Hello (); / / return myHello.SayHello () Throw new Exception ("I'll sabotage!") ;}}

Here we throw an exception by hand. Run the program below, and you will get the following results:

As shown in the figure above, when an exception occurs, not only the exception handling callback function is called automatically, but also the error message is easily obtained.

Thank you for your reading, the above is the content of "what is the use of ASP.NET AJAX". After the study of this article, I believe you have a deeper understanding of what the use of ASP.NET AJAX is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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