In general, if a request is successful, there will be a failure, and the asynchronous call will also be the same, and a request exception will occur. In traditional Ajax, the callback function is called no matter whether the request succeeds or fails. Then, we can determine the status of the XMLHttpRequest object to determine whether the request is successful. If the request fails, we can handle the exception.
However, in ASP. net ajax applications, the callback function is called only when the callback function is successful. What should I do if the callback function fails? 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: the callback function when the request is successful, and the callback function when the request fails)
However, in the previous application, we omitted the second callback function. To understand this, let's make a small experiment, open the ASPNETAJAXTest program in the previous article, and modify the content in the ajax. js file as follows:
Ajax. js:
- // Click the JS function called when btnSayHello is clicked.
- Function btnSayHello_onClick ()
- {
- SayHelloService. SayHello (OnSucceeded, OnFailded );
- }
- // Callback function when the call is successful
- Function OnSucceeded (reusltText)
- {
- $ Get ("result "). InnerHTML=ReusltText;
- }
- // Return function upon failure
- Function OnFailded (error)
- {
- $ Get ("result "). InnerHTML="Call failed. Error message :"+ Error. get_message ();
- }
When an exception occurs, the callback function has an error parameter, which is an object defined by the ASP. net ajax framework and contains the exception information. The get_message () method returns an exception string.
To see the effect, we also need to break the WebService to make the request abnormal. The "destroyed" WebService 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 (){
- // HelloMyHello=NewHello ();
- // Return myHello. SayHello ();
- Throw new Exception ("I will destroy it! ");
- }
- }
Here we throw an exception manually. Run the program below and get the following results:
For example, when an exception occurs, not only the exception handling callback function is automatically called, but the error information is also conveniently obtained.
This article describes the general method, basic principle, and Exception Handling for WebService calling in JavaScript, and answers the questions left in the previous article. However, there is another issue that is not discussed: the backend methods we call do not have any parameters so far. If there is a parameter, ASP. net ajax can realize automatic conversion of JavaScript variable types and C # variable types? What if it is a complex type? Such as object classes and various objects. Further, if so, how should we do it? We will discuss this issue in the next article.
- Introduction to ASP. net mvc Framework
- Introduction to MvcAjaxPanel in ASP. NET MVC
- ASP. net mvc Framework to save UpdatePanel
- Use ASP. net mvc source code to find a solution
- ActionInvoker of ASP. net mvc Framework