Mega Code Archive

 
Categories / JavaScript DHTML / Mootools
 

Request is the basic XHR request class in MooTools

<!-- MooTools is released under the Open Source MIT license,  which gives you the possibility to use it and modify  it in every circumstance.  --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <style type="text/css"> #result {   width: 500px;   padding: 10px;   background-color: #F9F9F9;   border-right: 1px solid #ccc;   border-bottom: 1px solid #ccc; }        </style>   <script type="text/javascript" src="mootools.js"></script>   <script type="text/javascript"> window.addEvent('domready', function(){   // You can skip the following line. We need it to make sure demos   // are runnable on MooTools demos web page.   var demo_path = window.demo_path || '';   // --   //We can use one Request object many times.   var req = new Request({     url: demo_path + 'data.txt',     onSuccess: function(txt){       $('result').set('text', txt);     },     // Our request will most likely succeed, but just in case, we'll add an     // onFailure method which will let the user know what happened.     onFailure: function(){       $('result').set('text', 'The request failed.');     }   });   $('makeRequest').addEvent('click', function(){     req.send();   });   $('failedRequest').addEvent('click', function(e){     //We can pass new options for our Request object to the send method.     req.send({url:'/assets/not_here.txt'});   }); });      </script>   <title>Request Demo</title> </head> <body>   <h1>Request</h1>   <h2>Introduction</h2>   <p>     Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTML and Request.JSON.   </p>   <p>     <a href="#" id="makeRequest" title="Make Request">Make Request</a> | <a href="#" id="failedRequest">This Request Will Fail</a>   </p>   <h2>Result</h2>   <div id="result">Waiting for the request to happen.</div> </body> </html>