To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object. This process allows your application to fetch or submit data without interrupting the user experience.
Select a method and click "Send Request" to see how the request is structured internally:
* GET is best for simple data fetching and is cached by browsers.
The open() method specifies the type of request. It takes three parameters:
GET or POST.true (asynchronous) or false (synchronous).xhttp.open("GET", "ajax_info.txt", true);
Choosing the right method is critical for security and performance:
| Comparison | GET | POST |
|---|---|---|
| Security | Less secure (data in URL) | More secure (data in body) |
| Data Limit | Limited (can be long URLs) | Unlimited |
| Caching | Can be cached | Never cached |
The send() method sends the request to the server.
xhttp.send().xhttp.send(string).If you need to send data like a form (via POST), you must add an HTTP header using setRequestHeader().
xhttp.open("POST", "ajax_test.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
true.