Ajax for beginners

Source: Internet
Author: User

The so-called Ajax is (Asynchronous JavaScript and XML) Asynchronous JavaScript and XML: First, let's talk about the problems we encounter if I don't feel like Ajax is available. For example, when we watch a video, let's take a look at the following comments, when we click the next page, the video starts again! So Ajax is required in some ways!

Next we will go to the question. We usually use 'General processing page' to process Ajax (*. ashx) in the client, JavaScript is used to process data that needs to be transferred to or from the server (this is my understanding). Below is a very good Ajax model written by myself: on the general processing pageCode:

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. Services; Namespace  Webapplication1 {  ///   <Summary>      ///  Handler Summary  ///   </Summary> [WebService (namespace = "  Http://tempuri.org/  "  )]  //  A WebService is referenced to use datatime [Webservicebinding (conformsto =Wsiprofiles. basicprofile1_1)]  //  Obtain the Web Service interoperability (WSI) Specification      Public   Class  Handler: ihttphandler {  Public   Void  Processrequest (httpcontext context) {context. response. contenttype = "  Text/plain  "  ; Context. response. Write (datetime. Now. tostring ()); //  Pass data in write () to the client  }  Public   Bool  Isreusable {  Get  {  Return   False  ;}}}} 

The following is the client code:

<! Doctype HTML public "  -// W3C // dtd xhtml 1.0 transitional // en  "  "  Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd  " > <HTML xmlns = "  Http://www.w3.org/1999/xhtml  " > <Head> <title> </title> <SCRIPT type = "  Text/JavaScript  " > Function butclick (){  VaR Httprequest = Null  ; //  Initialize the XMLHTTPRequest object              If  (Window. XMLHttpRequest ){  //  Create XMLHttpRequest objects in modern browsers such as Firefox Httprequest = New  XMLHttpRequest ();}  Else   If  (Window. activexobject ){  //  Create an XMLHTTPRequest object in IE Httprequest =New Activexobject ( "  Microsoft. XMLHTTP  "  );}  If (! Httprequest) {alert (  "  An error occurred while creating the httprequest object!  "  );} Httprequest. Open (  "  Post  " , " Handler. ashx  " , True  );  //  Httprequest sends a POST request to handler. The final parameter is to set whether the request is asynchronous. True is asynchronous, and false is synchronous. Httprequest. onreadystatechange = Function (){  //  Specifies the function corresponding to the onreadystatechange event handle.                  If (Httprequest. readystate = 4  ){  // 4 indicates that the server return is complete.                      If (Httprequest. Status = 200  ){  //  200 indicates successful Document. getelementbyid ( "  Text1  " ). Value = Httprequest. responsetext;  //  Responsetext indicates the text returned by the server. Another method is responsexml to obtain the XML returned by the server.  } Else  {Alert (  "  The Ajax server returns an error!  "  ) ;}} Httprequest. Send ();  //  Only in this case can requests be sent to the server.  } </SCRIPT>  "  Text1  " Type = "  Text " /> <Input id = "  Button1  " Type = "  Button  " Value = "  Button  " Onclick = "  Butclick ()  " /> </Body> 

Running result:

 

There are comments in the code. I hope beginners can understand them like me. If you have read any comments, leave a message. Thank you!

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.