responseText !== undefined){
document.getElementById('message').innerHTML = transport.responseText;
}
}
var handleFailure = function() {
document.getElementById('message').innerHTML = "";
}
function check(name)
{
var sUrl = "ajax.php?name=" + name;
new Ajax.Request(sUrl, #1
{
method: 'get', |#2
onSuccess: handleSuccess, |
onFailure: handleFailure |
}
);
}
(annotation)<#1 Prototype supplies the Ajax object which takes two parameters: the url and a configuration array>
Although there is a little more code than with our simple example, this time we have some error checking
and also have separated the functionality out. The prototype object that wraps XMLHttpRequest is called Ajax
and is created in the check() function. The constructor (#1) takes two arguments: the URL to connect to and a
configuration object. The configuration object is defined directly within the function call as an anonymous
object using the {} syntax (#2). (In some ways JavaScript takes the concept of dynamic in dynamic languages
to a whole new level!)
To configure our Ajax request, we tell prototype that we want to use a GET HTTP call and we register two
callback functions for success and failure. The code in handleSuccess() is very simple. The function receives a
transport object that encapsulates all that we know about the returned data, so all we have to do is extract the
responseText and assign it to the placeholder element.
Pages:
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132