- MooTools 1.3 Cookbook
- Jay Larry G. Johnston
- 293字
- 2021-04-02 19:07:07
Making an Ajax call
Getting ready
Asynchronous JavaScript and XML (Ajax) saves time by updating a page without refreshing it completely. Ajax has been around for a long time, with varied limits of browser support. It is safe to say that Google is most guilty of showing the world how easy it is to use.
How to do it...
Now with the MooTools abstraction layer's Request object, Ajax is child's play.
<script type="text/javascript" src="mootools-1.3.0.js"></script> </head> <body> <form action="" method="get"> <input type="button" id="mybutton" value="Ajax!" onclick="ajax_it();"/> </form> <script type="text/javascript"> var myJax = new Request({ url: '?', onSuccess: function(response) { alert('Success! Here is the response: ' +response); } }); function ajax_it() { myJax.send(); } </script>
How it works...
Switch the url
property to that of the script that will process the Ajax request. Like any good recipe, it is important to switch out ingredients as the chef's needs arise, for instance, artificial sweeteners for sugar. In this snippet, the url
variable, or to be more semantically correct, property
of the myJax
object is currently set to ?
. This probably only has purpose in this academic setting. The usage in this case causes this page to request itself, and the result is that the page calls its own URL and displays the resulting source code.
There's more...
One great example of how to use Ajax to make our web pages more friendly is in conjunction with a user sign-up form. It can be frustrating for users to submit their form only to find out their desired username is in use.
Program the sign-up form's input field to make an Ajax call onkeyup
. The returning value would be a Boolean that could be used to notify the user whether their choice was available or in use.
See also
Chapter 5, Mr. Clean Uses Ajax: Remote Asynchronous Calls is complete with Request(), Request.HTML
, and Request.JSON
examples.
- Expert Cube Development with Microsoft SQL Server 2008 Analysis Services
- Hadoop核心技術
- CAD/CAM軟件應用技術
- 通達信炒股軟件從入門到精通(第2版)
- Photoshop CC 2017從入門到精通
- 深入理解OpenCV:實用計算機視覺項目解析(原書第3版)
- PowerPoint 2016實戰從入門到精通(超值版)
- Photoshop CS6從入門到精通
- SOA Patterns with BizTalk Server 2009
- Apache Solr 3.1 Cookbook
- 網店美工設計:Photoshop CC案例教程(視頻指導版)
- After Effects 2022從新手到高手
- AI繪畫與攝影實戰108招:ChatGPT+Midjourney+文心一格
- Adobe創意大學Premiere Pro產品專家認證標準教材(CS6修訂版)
- 中文版UG NX 7.0基礎教程