Académique Documents
Professionnel Documents
Culture Documents
HOME
HTML
C SS
JAVASC RIPT
JQUERY
XML
ASP.NET
PHP
SQL
MORE...
R EFER ENC ES
EXAMPLES
FO R UM
ABO UT
Get Certified
Study Web Technologies and get a diploma at w3schools.com
Like
77k
AJAX Basic
AJAX HOME AJAX Intro AJAX Example
WEB HOSTING
Next Chapter
Best Web Hosting eUK Web Hosting UK Reseller Hosting Domain, Hosting & Email
AJAX XMLHttpRequest
XHR Create Object XHR Request XHR Response XHR readyState
AJAX Advanced
AJAX ASP/PHP AJAX Database AJAX XML File
STATISTICS
Browser Statistics OS Statistics Display Statistics
AJAX Examples
AJAX Examples
GET or POST?
GET is simpler and faster than POST, and can be used in most cases. However, always use POST requests when: A cached file is not an option (update a file or database on the server) Sending a large amount of data to the server (POST has no size limitations) Sending user input (which can contain unknown characters), POST is more robust and secure than GET
GET Requests
A simple GET request:
Example
x m l h t t p . o p e n ( " G E T " , " d e m o _ g e t . a s p " , t r u e ) ; x m l h t t p . s e n d ( ) ;
Try it yourself In the example above, you may get a cached result. To avoid this, add a unique ID to the URL:
Example
x m l h t t p . o p e n ( " G E T " , " d e m o _ g e t . a s p ? t = "+M a t h . r a n d o m ( ) , t r u e ) ; x m l h t t p . s e n d ( ) ;
Try it yourself If you want to send information with the GET method, add the information to the URL:
Example
x m l h t t p . o p e n ( " G E T " , " d e m o _ g e t 2 . a s p ? f n a m e = H e n r y & l n a m e = F o r d " , t r u e ) ; x m l h t t p . s e n d ( ) ;
Try it yourself
POST Requests
A simple POST request:
Example
x m l h t t p . o p e n ( " P O S T " , " d e m o _ p o s t . a s p " , t r u e ) ;
w3schools.com/ajax/ajax_xmlhttprequest_send.asp
1/3
8/8/13
x m l h t t p . s e n d ( ) ;
Try it yourself
To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:
Example
x m l h t t p . o p e n ( " P O S T " , " a j a x _ t e s t . a s p " , t r u e ) ; x m l h t t p . s e t R e q u e s t H e a d e r ( " C o n t e n t t y p e " , " a p p l i c a t i o n / x w w w f o r m u r l e n c o d e d " ) ; x m l h t t p . s e n d ( " f n a m e = H e n r y & l n a m e = F o r d " ) ;
Try it yourself
Method setRequestHeader(header,value)
Description Adds HTTP headers to the request. header: specifies the header name value: specifies the header value
Async=true
When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:
Example
x m l h t t p . o n r e a d y s t a t e c h a n g e = f u n c t i o n ( ) { i f( x m l h t t p . r e a d y S t a t e = = 4& &x m l h t t p . s t a t u s = = 2 0 0 ) { d o c u m e n t . g e t E l e m e n t B y I d ( " m y D i v " ) . i n n e r H T M L = x m l h t t p . r e s p o n s e T e x t ; } } x m l h t t p . o p e n ( " G E T " , " a j a x _ i n f o . t x t " , t r u e ) ; x m l h t t p . s e n d ( ) ;
Try it yourself You will learn more about onreadystatechange in a later chapter.
Async=false
To use async=false, change the third parameter in the open() method to false:
Example
x m l h t t p . o p e n ( " G E T " , " a j a x _ i n f o . t x t " , f a l s e ) ; x m l h t t p . s e n d ( ) ; d o c u m e n t . g e t E l e m e n t B y I d ( " m y D i v " ) . i n n e r H T M L = x m l h t t p . r e s p o n s e T e x t ;
Try it yourself
w3schools.com/ajax/ajax_xmlhttprequest_send.asp
2/3
8/8/13
Previous
Next Chapter
Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial
Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML C olors
Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples
Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz
Color Picker
Statistics
Browser Statistics Browser OS Browser Display
RE P O RT E RRO R
HO ME
TO P
P RI N T
FO RU M
A BO U T
A D V E RT I SE WI T H U S
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.
w3schools.com/ajax/ajax_xmlhttprequest_send.asp
3/3