An XMLHttpRequest Wrapper.
var myRequest = new Request([options]);
onTimeout
event, it determines the amount of milliseconds before considering a connection timed out. (It's suggested to not use timeout with big files and only when knowing what's expected.)script
tags inside the response will be evaluated.user
option to set authentication credentials when necessary. Note that the password will be passed as plain text and is therefore readable by anyone through the source code. It is therefore encouraged to use this option carefullyFired when the Request is sent.
onRequest()
Fired when the Request loaded, right before any progress starts. (This is limited to Browsers that support the event. At this time: Gecko and WebKit).
onLoadstart(event, xhr)
Fired when the Request is making progresses in the download or upload. (This is limited to Browsers that support the event. At this time: Gecko and WebKit).
onProgress(event, xhr)
var myRequest = new Request({ url: 'image.jpg', onProgress: function(event, xhr){ var loaded = event.loaded, total = event.total; console.log(parseInt(loaded / total * 100, 10)); } }); myRequest.send();
Fired when the Request is completed.
onComplete()
Fired when a request has been cancelled.
onCancel()
Fired when the Request is completed successfully.
onSuccess(responseText, responseXML)
Fired when the request failed (error status code).
onFailure(xhr)
xhr - (XMLHttpRequest) The transport instance.
Fired when setting a request header fails.
onException(headerName, value)
var myRequest = new Request({method: 'get', url: 'requestHandler.php'}); myRequest.send('name=john&lastname=dorian');
Fired when a request doesn't change state for options.timeout
milliseconds.
onTimeout()
This example fetches some text with Request. When the user clicks the link, the returned text by the server is used to update
the element's text. It uses the onRequest
, onSuccess
and onFailure
events to inform the user about the current state of
the request. The method
option is set to get
because we get some text instead of posting it to the server. It gets the
data-userid attribute of the clicked link, which will be used for the querystring.
var myElement = document.id('myElement'); var myRequest = new Request({ url: 'getMyText.php', method: 'get', onRequest: function(){ myElement.set('text', 'loading...'); }, onSuccess: function(responseText){ myElement.set('text', responseText); }, onFailure: function(){ myElement.set('text', 'Sorry, your request failed :('); } }); document.id('myLink').addEvent('click', function(event){ event.stop(); myRequest.send('userid=' + this.get('data-userid')); });
Add or modify a header for the request. It will not override headers from the options.
myRequest.setHeader(name, value);
var myRequest = new Request({url: 'getData.php', method: 'get', headers: {'X-Request': 'JSON'}}); myRequest.setHeader('Last-Modified', 'Sat, 1 Jan 2005 05:00:00 GMT');
Returns the given response header or null if not found.
myRequest.getHeader(name);
null
if the header is not found.var myRequest = new Request({url: 'getData.php', method: 'get', onSuccess: function(responseText, responseXML){ alert(this.getHeader('Date')); // alerts the server date (for example, 'Thu, 26 Feb 2009 20:26:06 GMT') }});
Opens the Request connection and sends the provided data with the specified options.
myRequest.send([options]);
var myRequest = new Request({ url: 'http://localhost/some_url' }).send('save=username&name=John');
MooTools provides several aliases for Request:send to make it easier to use different methods.
These aliases are:
post()
and POST()
get()
and GET()
put()
and PUT()
delete()
and DELETE()
myRequest.post([data]);
data
option of Request.var myRequest = new Request({url: 'http://localhost/some_url'}); myRequest.post('save=username&name=John'); //...is equivalent to: myRequest.send({ method: 'post', data: 'save=username&name=John' }); myRequest.get('save=username&name=John'); //...is equivalent to: myRequest.send({ method: 'get', data: 'save=username&name=John' });
By default the emulation option is set to true, so the put and delete send methods are emulated and will actually send as post while the method name is sent as e.g. _method=delete
.
Async
and timeout
options are mutually exclusive. If you set async
to true, then there's no need to set the timeout
since the server and browser will set their own timeouts to return executing the rest of your script.
Cancels the currently running request, if any.
myRequest.cancel();
var myRequest = new Request({url: 'mypage.html', method: 'get'}).send('some=data'); myRequest.cancel();
Returns true if the request is currently running
myRequest.isRunning()
var myRequest = new Request({url: 'mypage.html', method: 'get'}).send('some=data'); if (myRequest.isRunning()) // It runs!
Sets a default Request instance for an Element. This is useful when handling forms.
el.set('send'[, options]);
myForm.set('send', {url: 'contact.php', method: 'get'}); myForm.send(); //Sends the form.
Returns the previously set Request instance (or a new one with default options).
el.get('send');
el.set('send', {method: 'get'}); el.send(); el.get('send'); // returns the Request instance.
Custom Type to allow all of its methods to be used with any DOM element via the dollar function $.
Sends a form or a container of inputs with an HTML request.
myElement.send(url);
<form id="myForm" action="submit.php"> <p> <input name="email" value="bob@bob.com" /> <input name="zipCode" value="90210" /> </p> </form>
$('myForm').send();
© Linux.ria.ua, 2008-2024 |