AJAX stands for Asynchronous JavaScript and XML. It's a way of sending and receiving data between a web browser and a server without having to reload the entire page. AJAX is crucial in creating fast and dynamic web applications. AJAX is not a new technology, nor is it a programming language. It's just a technique that combines existing standards:
Advantages of AJAX:
The Basics of the XMLHttpRequest Object: XMLHttpRequest is the key object that AJAX uses for communication with the server. Here's a basic example to demonstrate its usage:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Action to be performed when the document is read;
console.log(this.responseText);
}
};
xhttp.open("GET", "url_to_fetch_data_from", true);
xhttp.send();
AJAX with JSON
Example: Most modern web applications use JSON rather than XML as the data format. Here's how you'd handle a JSON response:
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonResponse = JSON.parse(this.responseText);
console.log(jsonResponse);
}
};
Full AJAX
Example: Here's a simple example where we fetch some data (let's assume it's JSON data) from a URL and log it to the console:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var jsonResponse = JSON.parse(this.responseText);
console.log(jsonResponse);
}
};
xhttp.open("GET", "https://api.example.com/data", true);
xhttp.send();
Remember, while using AJAX, it's vital to ensure that proper error handling is implemented and that you are aware of cross-domain restrictions (Same-Origin Policy) which may prevent you from making requests to different domains than the one your script originates from. Solutions for this include using CORS (Cross-Origin Resource Sharing) headers on the server or utilizing JSONP (though the latter is less secure and becoming outdated).
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here