JavaScript XMLHttpRequest Object
JavaScript XMLHttpRequest Object
The XMLHttpRequest
(XHR) objects is a core of AJAX used to interact with servers. This helps you to retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without reloading the whole page.
Note: Despite its name, XMLHttpRequest
can be used to retrieve any type of data, not just XML.
Requesting a URL
In the following example, we request a URL and receive a response.
Example
Syntax
XMLHttpRequest Object Methods
Value | Explanation |
---|---|
abort() | Terminate the current request. |
getAllResponseHeaders() | Returns all the response headers |
getResponseHeader() | Returns the string containing the text of a particular header's value. |
new XMLHttpRequest() | Creates a new XMLHttpRequest. |
open(method,url,async,user,pass) | Initializes a newly-created request, or re-initializes an existing one. Parameters Explanation
|
send(data) | sends the request to the server. |
setRequestHeader() | Sets the value of an HTTP request header. |
XMLHttpRequest Object Properties
Value | Explanation |
---|---|
onreadystatechange | Specifies a function which is called from the user interface thread. |
readyState | Returns the state an XMLHttpRequest client is in. Possible values are
|
responseText | Returns the text received from a server following a request being sent. |
responseXML | Returns a Document containing the HTML or XML retrieved by the request. |
status | Returns the numerical HTTP status code of the XMLHttpRequest's response. |
statusText | Returns a DOMString containing the response's status message as returned by the HTTP server. |
Cross-origin resource sharing
For security reasons, browsers do not allow access across domains.
For example: From www.abc.com you can't send XMLHttpRequest to www.xyz.com.
All resources of www.abc.com can only be accessed by itself.
Reminder
Hi Developers, we almost covered 95% of AJAX Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in AJAX.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.