Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License.
http://server/path/file
http://science.slashdot.org/article.pl?sid=07/04/20/1651219
science.slashdot.org
to run the program article.pl
with certain parametershttp://www.google.com/search?q=colbert&ie=utf-8
http://server/path/program?query_string
field1=value1&field2=value2&field3=value3...
?
name=value
pairs separated by &
search
, with parameter q
set to colbert
and the parameter ie
set to utf-8
ascii.php
https://webster.cs.washington.edu
in the /stepp/ajax
foldername
specifying the student's last namehttps://webster.cs.washington.edu/stepp/ajax/ascii.php?name=stepp
XMLHttpRequest
object can fetch files from a web server
XMLHttpRequest
objectXMLHttpRequest
object requests a document from a web serverXMLHttpRequest
fires event to say that the data has arrived
XMLHttpRequest
objectthe core JavaScript object that makes Ajax possible
abort
, getAllResponseHeaders
, getResponseHeader
, open
, send
, setRequestHeader
onreadystatechange
, readyState
, responseText
, responseXML
, status
, statusText
ActiveXObject
instead// this code is in some control's event handler var ajax = new XMLHttpRequest(); ajax.open("get", url, false); ajax.send(null); do something with ajax.responseText;
send
returns, the fetched text will be stored in request's responseText
propertyvar ajax = new XMLHttpRequest(); ajax.onreadystatechange = function; ajax.open("get", url, true); ajax.send(null); // don't process ajax.responseText here, but in your function
onreadystatechange
eventreadyState
propertyXMLHttpRequest
readyState
property:
State | Description |
---|---|
0 | not initialized |
1 | set up |
2 | sent |
3 | in progress |
4 | complete |
readyState
changes → onreadystatechange
handler runsreadyState
of 4 (complete)XMLHttpRequest
templatevar ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { // 4 means request is finished do something with ajax.responseText; } }; ajax.open("get", url, true); ajax.send(null);
ajax
)var ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { if (ajax.status == 200) { // 200 means request succeeded do something with ajax.responseText; } else { code to handle the error; } } }; ajax.open("get", url, true); ajax.send(null);
new Ajax.Request( "url", { option : value, option : value, ... option : value } );
Ajax.Request
object constructor accepts 2 parameters:
{}
bracesonreadystatechanged
, etc.)Ajax.Request
constructor:
method
: how to fetch the request from the server (default "post"
)
parameters
: query parameters to pass to the server, if any
asynchronous
(default true
),
contentType
,
encoding
,
requestHeaders
Ajax.Request
object that you can handle:
onSuccess
: request completed successfully
onFailure
: request was unsuccessful
onCreate
,
onComplete
,
onException
,
on###
(handler for HTTP error code ###)
new Ajax.Request( "url", { method: "get", onSuccess: functionName } ); ... function functionName(ajax) { do something with ajax.responseText; }
onSuccess
eventXMLHttpRequest
object, ajax
, as a parameternew Ajax.Request( "url", { method: "get", onSuccess: functionName, onFailure: ajaxFailure } ); ... function ajaxFailure(ajax) { alert("Error making Ajax request:" + "\n\nServer status:\n" + ajax.status + " " + ajax.statusText + "\n\nServer response text:\n" + ajax.responseText); }
XMLHttpRequest
security restrictionswww.foo.com/a/b/c.html
can only fetch from www.foo.com
new Ajax.Updater( "id", "url", { method: "get" } );
Ajax.Updater
can be used if you want to fetch a file via Ajax and inject its text/HTML contents into an onscreen elementid
of the element into which to inject the content