AJAX - send() in AJAX
Let me explain the send()
method in AJAX step by step with syntax, parameters, examples, and best practices.
It works together with open()
to send a request to the server.
1. What is send()
in AJAX?
The send()
method is used to actually send the HTTP request to the server after calling open()
.
-
Without
send()
, the request will not be sent. -
It works for GET, POST, and other HTTP methods.
-
You can also pass data with
send()
when using POST or PUT requests.
2. Syntax
xhr.send(body);
Parameters
Parameter | Type | Description |
---|---|---|
body (optional) | String / null | The data you want to send to the server. For GET requests, use null or leave empty. For POST, pass form data, JSON, or query strings. |
3. How send()
Works in AJAX
-
Create XMLHttpRequest object
var xhr = new XMLHttpRequest();
-
Initialize request using
open()
xhr.open("GET", "data.txt", true);
-
Send the request
xhr.send();
-
Handle response
xhr.onload = function() { console.log(xhr.responseText); };
4. Example 1 — Using send()
with GET Request
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.txt", true); // Prepare request
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText); // Shows the file content
}
};
xhr.send(); // Send the request
Explanation:
-
No data is sent in GET.
-
send()
is called without parameters.
5. Example 2 — Using send()
with POST Request
var xhr = new XMLHttpRequest();
xhr.open("POST", "server.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText); // Server response
}
};
xhr.send("name=John&age=25"); // Sending form data to server
Explanation:
-
Here, we pass data to
send()
in the form of key=value pairs. -
We also set a Content-Type header for proper encoding.
6. Example 3 — Sending JSON Data
var xhr = new XMLHttpRequest();
xhr.open("POST", "api.php", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function() {
console.log(xhr.responseText);
};
var data = JSON.stringify({ name: "John", age: 25 });
xhr.send(data); // Sending JSON data
Explanation:
-
Use
JSON.stringify()
to convert JavaScript objects into JSON. -
Set
Content-Type
toapplication/json
.
7. Example 4 — Sending No Data (GET)
var xhr = new XMLHttpRequest();
xhr.open("GET", "users.json", true);
xhr.send(); // Empty request body
Explanation:
-
For GET requests,
send()
is usually called empty.
8. Relationship Between open()
and send()
Step | Method | Purpose |
---|---|---|
1 | open() |
Initializes the request (method, URL, async) |
2 | setRequestHeader() (optional) |
Sets headers (for POST/PUT) |
3 | send() |
Sends the request to the server |
4 | onload / onreadystatechange |
Handles the server response |
Rule:
Always call
send()
afteropen()
.
9. Key Points to Remember
-
send()
is mandatory to send an AJAX request. -
For GET → Call
send()
with no data. -
For POST → Pass data inside
send()
. -
Use
setRequestHeader()
only afteropen()
but beforesend()
. -
Use
JSON.stringify()
when sending JSON.