Introduction to ASP. NET AJAX

Source: Internet
Author: User

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:

 
 
  1. // Click the JS function called when btnSayHello is clicked.
  2. Function btnSayHello_onClick ()
  3. {
  4. SayHelloService. SayHello (OnSucceeded, OnFailded );
  5. }
  6. // Callback function when the call is successful
  7. Function OnSucceeded (reusltText)
  8. {
  9. $ Get ("result "). InnerHTML=ReusltText;
  10. }
  11. // Return function upon failure
  12. Function OnFailded (error)
  13. {
  14. $ Get ("result "). InnerHTML="Call failed. Error message :"+ Error. get_message ();
  15. }


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:

 
 
  1. SayHelloService. cs:
  2. Using System;
  3. Using System. Web; using System. Collections;
  4. Using System. Web. Services;
  5. Using System. Web. Services. Protocols; using System. Web. Script. Services;
  6. [WebService (Namespace="Http://tempuri.org /")]
  7. [WebServiceBinding (ConformsTo=WsiProfiles. BasicProfile1_1)]
  8. [ScriptService] public class SayHelloService
  9. System. Web. Services. WebService {public SayHelloService (){
  10. }
  11. [WebMethod] public string SayHello (){
  12. // HelloMyHello=NewHello ();
  13. // Return myHello. SayHello ();
  14. Throw new Exception ("I will destroy it! ");
  15. }
  16. }


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.

  1. Introduction to ASP. net mvc Framework
  2. Introduction to MvcAjaxPanel in ASP. NET MVC
  3. ASP. net mvc Framework to save UpdatePanel
  4. Use ASP. net mvc source code to find a solution
  5. ActionInvoker of ASP. net mvc Framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.