AJAX – Send to server
AJAX - Send request to server
The XMLHttpRequest object is used to exchange data with the server.
Send request to server
To send a request to the server, we useXMLHttpRequestobject'sopen()andsend()method:
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
| method | description |
|---|---|
| open(method,url,async) |
Specifies the request type, URL, and whether to process the request asynchronously. |
Sends the request to the server.
- string: Used only for POST requests
GET or POST?
Compared to POST, GET is simpler and faster, and can be used in most cases.
However, use a POST request in the following situations:
- When you do not want to use cached files (updating files or databases on the server)
- When sending a large amount of data to the server (POST has no data limit)
- When sending user input containing unknown characters, POST is more stable and reliable than GET

GET request
A simple GET request:
Instance
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/demo_get.php
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try »
In the example above, you might get a cached result.
To avoid this situation, please add a unique ID to the URL:
Instance
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/demo_get.php?t=
"
Math
.
random
(
)
,
true
)
;
xmlhttp
.
send
(
)
;
Try »
If you want to send information using the GET method, add the information to the URL:
instance
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/demo_get2.php?fname=Henry&lname=Ford
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try it »
POST request
A simple POST request:
instance
xmlhttp
.
open
(
"
POST
"
,
"
/try/ajax/demo_post.php
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try »
If you need to POST data like an HTML form, use setRequestHeader() to add HTTP headers. Then specify the data you want to send in the send() method:
instance
xmlhttp
.
open
(
"
POST
"
,
"
/try/ajax/demo_post2.php
"
,
true
)
;
xmlhttp
.
setRequestHeader
(
"
Content-type
"
,
"
application/x-www-form-urlencoded
"
)
;
xmlhttp
.
send
(
"
fname=Henry&lname=Ford
"
)
;
Try »
| Method | Description |
|---|---|
| setRequestHeader(header,value) |
Adds an HTTP header to the request. |
url - file on the server
of the open() methodurlThe parameter is the address of the file on the server:
xmlhttp.open("GET","ajax_test.html",true);
This file can be any type of file, such as .txt and .xml, or a server script file, such as .asp and .php (to execute tasks on the server before returning the response).
Asynchronous - True or False?
AJAX refers to Asynchronous JavaScript and XML.
If the XMLHttpRequest object is to be used for AJAX, its open() method's async parameter must be set to true:
xmlhttp.open("GET","ajax_test.html",true);
For web developers, sending asynchronous requests is a huge improvement. Many tasks executed on the server are quite time-consuming. Before AJAX, this could cause the application to hang or stop.
With AJAX, JavaScript does not have to wait for the server's response, but rather:
- Execute other scripts while waiting for the server response
- Process the response when it is ready
Async=true
When using async=true, specify the function to execute when the response is in the ready state of the onreadystatechange event:
Instance
xmlhttp
.
onreadystatechange
=
function
(
)
{
if
(
xmlhttp
.
readyState
==
4
&&
xmlhttp
.
status
==
200
)
{
document
.
getElementById
(
"
myDiv
"
)
.
innerHTML
=
xmlhttp
.
responseText
;
}
}
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/ajax_info.txt
"
,
true
)
;
xmlhttp
.
send
(
)
;
Try it »
You will learn more about onreadystatechange in later chapters.
Async = false
If you need to use async=false, change the third parameter in the open() method to false:
xmlhttp.open("GET","test1.txt",false);
We do not recommend using async=false, but it is acceptable for some small requests.
Remember that JavaScript will wait for the server response to be ready before continuing execution. If the server is busy or slow, the application will hang or stop.
**Note:** When using async=false, do not write an onreadystatechange function - place the code after the send() statement:
Instance
xmlhttp
.
open
(
"
GET
"
,
"
/try/ajax/ajax_info.txt
"
,
false
)
;
xmlhttp
.
send
(
)
;
document
.
getElementById
(
"
myDiv
"
)
.
innerHTML
=
xmlhttp
.
responseText
;
Try it »