Vous êtes sur la page 1sur 3

8/8/13

AJAX Send an XMLHttpRequest To a Server


Search w3schools.com: Select Language

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

SHARE THIS PAGE

Like

77k

AJAX Basic
AJAX HOME AJAX Intro AJAX Example

AJAX - Send a Request To a Server


Previous
The XMLHttpRequest object is used to exchange data with a server.

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

Send a Request To a Server


To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object: WEB BUILDING
Download XML Editor

AJAX Advanced
AJAX ASP/PHP AJAX Database AJAX XML File

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 ( ) ;


Method open(method,url,async) Description Specifies the type of request, the URL, and if the request should be handled asynchronously or not. method: the type of request: GET or POST url: the location of the file on the server async: true (asynchronous) or false (synchronous) send(string) Sends the request off to the server. string: Only used for POST requests

FREE Website BUILDER FREE Website C reator Best Website Templates

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

AJAX Send an XMLHttpRequest To a Server

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

The url - A File On a Server


The url parameter of the open() method, is an address to a file on a server:

x m l h t t p . o p e n ( " G E T " , " a j a x _ t e s t . a s p " , t r u e ) ;


The file can be any kind of file, like .txt and .xml, or server scripting files like .asp and .php (which can perform actions on the server before sending the response back).

Asynchronous - True or False?


AJAX stands for Asynchronous JavaScript and XML, and for the XMLHttpRequest object to behave as AJAX, the async parameter of the open() method has to be set to true:

x m l h t t p . o p e n ( " G E T " , " a j a x _ t e s t . a s p " , t r u e ) ;


Sending asynchronous requests is a huge improvement for web developers. Many of the tasks performed on the server are very time consuming. Before AJAX, this operation could cause the application to hang or stop. With AJAX, the JavaScript does not have to wait for the server response, but can instead: execute other scripts while waiting for server response deal with the response when the response ready

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:

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 ) ;


Using async=false is not recommended, but for a few small requests this can be ok. Remember that the JavaScript will NOT continue to execute, until the server response is ready. If the server is busy or slow, the application will hang or stop. Note: When you use async=false, do NOT write an onreadystatechange function - just put the code after the send() statement:

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

AJAX Send an XMLHttpRequest To a Server

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

Vous aimerez peut-être aussi