Ajax Getting Started Guide (iv)

Source: Internet
Author: User
Tags object count end http request time interval client javascript settimeout
An introductory guide to Ajax learning should help Ajax beginners.

Prototype support for Ajax
    ajax.request class
    The following code is an example:
    <!--client: Index.htm-->
    <script language= "javascript" type= text/javascript "src=" Prototype1.6.js "></script>
    <script language=" javascript "
          function Test () {
          var myajax = new Ajax.request (
         ' data.html ',
          {
            Method: ' Get ',
            oncomplete:showresponse
        }
       );
        }
        &nbsP function Showresponse (response) {
             $ (' Mydiv '). Innerhtml=response.responsetext;
         }
    </script>
        <body>
        <input type= "button" value= "click" onclick= "Test ()"/>
         <div id= "mydiv"/>
    </body>
    

    <!--server: Data.html-->
        < Head>
        <meta http-equiv= "Content-type content=" text/html; charset=gb2312 "
        <script language=" javascript "type=" text/ JavaScript "src=" Prototype1.6.js "></script>
               <body>
             ajax.request DEMO
        </body>
        

        Ajax Action option attribute meaning:
        method                      HTTP request method (Post/get/head)
        parameters               the list of values in the URL format passed in to the HTTP request, which is the part of the URL string after the question mark
         asynchronous          Do you want to make an asynchronous XMLHttpRequest request
        postbody                 in the POST request mode, the contents of the incoming request body
         requestheaders       A list of HTTP headers to be passed along with the request, which must contain even several items
         onxxxxxxx        in the process of HTTP request, response, The response that is invoked when the state of the XMLHttpRequest object has changedfunction The response function has 5:onuninitialized,  onloading,  onloaded,  oninteractive and    oncomplete
         onsuccess                The response function that is invoked when the Ajax operation completes successfully, and the incoming arguments are the same as Onxxxxxx
        onfailure                The response function that is invoked when an AJAX operation request completes, but an error occurs, and the passed parameter is the same as onxxxxxx
        onexception            The response function that is invoked when an exception occurs in an AJAX operation that can receive two parameters, and the 1th parameter is the XMLHttpRequest object that executes the HTTP request , the 2nd parameter is an exception object

Ajax.updater class


The following code example:


&lt;!--client:index.htm--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html;charset=gb2312" &gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;script language= "JavaScript" &gt;


function Test () {


var myajax = new Ajax.updater (


' Mydiv ',


' data.html ',


                             {


method: ' Get '


                             }


);


             } 


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;input type= "button" value= "click" onclick= "Test ()"/&gt;


&lt;div id= "mydiv"/&gt;


&lt;/body&gt;


&lt;/html&gt;


&lt;!--service End:data.html--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html;charset=gb2312" &gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


ajax.request DEMO


&lt;/body&gt;


&lt;/html&gt;

In addition, the Ajax.updater class has another feature that, if the requested page content includes a Javascript,ajax.updater class that can execute the script, only in the Ajax operation options Adding a property "Evalscripts:true" is OK. The above example is modified to get the following example:


&lt;!--client:index.htm--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html;charset=gb2312" &gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;script language= "JavaScript" &gt;


function Test () {


var myajax = new Ajax.updater (


' Mydiv ',


' Data.html ',


                             { 


method: ' Get ',


evalscripts:true


}


                  );


             } 


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;input type= "button" value= "click" onclick= "Test ()"/&gt;


&lt;div id= "mydiv"/&gt;


&lt;/body&gt;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/html&gt;

    <!--server: Data.html-->
        < Head>
        <meta http-equiv= "Content-type content=" text/html; charset=gb2312 "
        <script language=" javascript "type=" text/ JavaScript "src=" Prototype1.6.js "></script>
        <script language= "JavaScript" type= "Text/javascript"
              sayhi = function () {
                   alert ("Heelo," + $F (' name ') + "!");
            }
        </script>
         <body>
        <Input type= "text" id= "name" value= "Ajax.updater DEMO"/>
        <input Type= "button" value= "click Me" onclick= "Sayhi ()"/>
    </body>
   

If we change the function of Sayhi blackbody in data.html to: function sayhi{...} or var sayhi = function () {...}, the program will not function correctly. This is because Ajax.updater executes the script in an eval way, rather than introducing the script content to the current page, declaring the scope of the Sayhi function declared with Var only within this script, outside of which other scripts cannot be accessed, The scope of the sayhi described in bold is the entire window.

Ajax.periodupdater class


in some AJAX applications, some page elements need to be updated periodically, such as weather forecasts, news, and so on. Implementing such a function usually uses the timer function in JavaScript settimeout, cleartimeout, and so on, with Ajax.peri The Odupdater class can be greatly simplified.


you need to specify 3 parameters to create an instance of a Ajax.periodupdater class:


container: The page element ID that will be updated;


URL: The URL address of the request;


Options:ajax operation Options

Like the
and Ajax.updater classes, the Ajax.periodupdater class also supports the dynamic execution of JavaScript scripts by adding (evalscripts:true) property values to the Ajax operation options.


The Ajax.periodupdater class supports two special AJAX operation options, frequency and decay. The frequency parameter is easy to understand, is to update the page element regularly, or to execute the script regularly, frequency refers to the time interval between two Ajax operations, in seconds, the default value is 2


The following code is an example:


&lt;!--client:index.htm--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;script language= "JavaScript" &gt;


function Test () {


var myajax = new Ajax.periodicalupdater (


' Mydiv ',


' data.html ',


                     {


method: ' Get ',


Evalscripts:true,


Frequency:1,


Decay:2


                     }


                   );


                 }


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;input type= "button" value= "click" onclick= "Test ()"/&gt;


&lt;div id= ' mydiv '/&gt;


&lt;/body&gt;


&lt;/html&gt;


       


&lt;!--service End:data.html--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;script language= "javascript" type= "Text/javascript" &gt;


count=9;//Manually change the number, we can see the value of index.htm page mydiv element being updated at the same time


$ (' MyDiv1 '). InnerHTML = "Count =" + Count + ":" + new Date () + "&lt;br&gt;";


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;div id= ' myDiv1 '/&gt;


&lt;/body&gt;


&lt;/html&gt;


Ajax.responders Object


This class maintains a list of running Ajax objects that you can use when you need to implement some global functionality. For example, after an AJAX request is issued, you need to prompt the user that the action is in progress, and then cancel the prompt after the operation, as follows:


&lt;!--client:index.htm--&gt;


&lt;html&gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html;charset=gb2312" &gt;


&lt;script language= "javascript" type= "Text/javascript" src= "Prototype1.6.js" &gt;&lt;/script&gt;


&lt;script language= "JavaScript" &gt;


function Test () {


var myajax = new Ajax.request (


' data.html ',


                                 {


method: ' Get ',


Oncomplete:showresponse


                                 }


                      );


                 } 


function Showresponse (response) {


$ (' mydiv '). Innerhtml=response.responsetext;


                  }


var handle={


oncreate:function () {


element.show (' Loading '); When creating an AJAX request, display loading


},


oncomplete:function () {


/////When the request returns successfully, hide loading
If there are no other AJAX requests running at the moment

if (Ajax.activerequestcount = = 0) {


Element.hide (' loading ');


                          }


                 }


      };


//registers the handle into the global Ajax.responders object to take effect


Ajax.Responders.register (handle);


&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;input type= "button" value= "click" onclick= "Test ()"/&gt;


&lt;div id= "mydiv"/&gt;


&lt;div id= "Loading" style= "Display:none" &gt;


&lt;img src= "Loading.gif" &gt;loading ...


&lt;/div&gt;


&lt;/body&gt;


&lt;/html&gt;

<!--service End:data.html-->

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<body>
Ajax.responders DEMO
</body>

This defines a handle object that contains the OnCreate and OnComplete functions that invoke the OnCreate method when any Ajax request is made in the page, and the OnComplete method is invoked when the request completes.







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.