mootools tutorial (a): Exploration of mootools ajax application

Before using mootools ajax application, you should make sure you have downloaded the following files:
Moo.js, function.js, Array.js, String.js, Element.js, ajax.js
the bottom on the download page of mootools.net there are three source code format:

Packer Compression: compressed version is not suitable for learning No Documentation: just remove the document to explain the No Compression: This is a document that contains explanation for the use of the learning phase.

Use 'ajax'class
we look at the' ajax 'class source code:

var Ajax = ajax = new Class({
setOptions: function(options){
this.options = {
method: ‘post’,
postBody: ”,
async: true,
onComplete: Class.empty,
update: null,
evalScripts: false
};
Object.extend(this.options, options || {});
},
initialize: function(url, options){
this.setOptions(options);
this.url = url;
this.transport = this.getTransport();
},

You can see that this class is initialized by the new Ajax (url, options), url is the path to the file in your server's
syntax:

new Ajax(url,options).request();

or

var ajax = new Ajax (url, options);
Ajax.Request ();

Here is a simple example of the use ajax.js:
New ajax.php

<body>
<div id=”content”>Welcome to Fackweb’s Blog</div>
</body>

js code:

new ajax(’test.php’,{onComplete: showResponse, update: ‘content’}).request();
function showResponse(request){
alert(request);
}

Of course, you may also like this:

new ajax(’test.html’,{onComplete: function(request){$(’content’).innerHTML=request}}).request();

Here you can update: 'content' function to remove because after he and onComplete do the same thing.

There is a pass parameters:

new ajax(’test.php’,{
postBody:’name=fackweb&sex=’man”,
update:’content’}
}).request();

Here is an example of two transfer form parameters:

New form:

<form action=”test.php” id=”form1″>
<input name=”nickname” />
<input name=”sex” />
<input type=”button” onclick=”ajaxRequest()” />
</form>

Example 1:

ajaxRequest function () {
var postArgs = $ ( 'form1') toQueryString ();.
// acquisition parameters to form form1 name = fackweb & sex = man format
new new Ajax ( 'test.php', {
postBody: postArgs,
Update: 'Content'}
) .request ();
return to false;
}

Example 2:

function ajaxRequest(){
$(’form1′).send({update:’content’});
return false;
}}

Note the difference between these two examples:
use cases without specific action, In the form 1, Ajax url must specify in
Example 2 in the form must be specified action, without specifying in Ajax url
further accepts all the parameters used in the test.php $ _POST [ "] to accept the

Reproduced in: https: //www.cnblogs.com/200831856/articles/mootools.html

Guess you like

Origin blog.csdn.net/weixin_33825683/article/details/93711110