AJAX
Asynchronous JavaScript and XML (Ajax, or AJAX)
a web development technique in which a web app fetches content from the server
by making asynchronous HTTP requests,
and uses the new content to update the relevant parts of the page without
requiring a full page load.
This can make the page more responsive, because only the parts that need to be
updated are requested.
Ajax can be used to create single-page apps
Initially Ajax was implemented using the XMLHttpRequest interface,
but the fetch() API is more suitable for modern web applications:
XMLHttpRequest
objects are used to interact with servers
This enables a Web page to update just part of a page without disrupting what the
user is doing.
Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just
XML.
Constructor
The constructor initializes an
XMLHttpRequest() XMLHttpRequest . It must be called
before any other method calls.
The XMLHttpRequest() constructor creates a new XMLHttpRequest .
AJAX 1
Syntax
new XMLHttpRequest()
Return value
A new XMLHttpRequest object.
The object must be prepared by at least calling open() to initialize it before
calling send() to send the request to the server.
Create an XMLHttpRequest Object
variable = new XMLHttpRequest();
XMLHttpRequest Object Methods
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
Specifies the requestmethod: the request type GET or
open(method, url, async, POSTurl: the file locationasync: true (asynchronous) or false
user, psw) (synchronous)user: optional user namepsw: optional
password
send() Sends the request to the serverUsed for GET requests
send(string) Sends the request to the server.Used for POST requests
setRequestHeader() Adds a label/value pair to the header to be sent
AJAX 2
XMLHttpRequest Object Properties
Property Description
onload Defines a function to be called when the request is received (loaded)
Defines a function to be called when the readyState property
onreadystatechange
changes
Holds the status of the XMLHttpRequest.0: request not initialized1:
readyState server connection established2: request received3: processing
request4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
Returns the status-number of a request200: "OK"403:
status "Forbidden"404: "Not Found"For a complete list go to the Http
Messages Reference
statusText Returns the status-text (e.g. "OK" or "Not Found")
The onreadystatechange Property
The readyState property holds the status of the XMLHttpRequest.
The onreadystatechange property defines a callback function to be executed when
the readyState changes.
The status property and the statusText properties hold the status of the
XMLHttpRequest object.
Property Description
Defines a function to be called when the readyState property
onreadystatechange
changes
Holds the status of the XMLHttpRequest.0: request not initialized1:
readyState server connection established2: request received3: processing
request4: request finished and response is ready
200: "OK"403: "Forbidden"404: "Page not found"For a complete list
status
go to the Http Messages Reference
statusText Returns the status-text (e.g. "OK" or "Not Found")
AJAX 3
The onreadystatechange function is called every time the readyState changes.
When readyState is 4 and status is 200, the response is ready:
AJAX 4