Objekt XMLHttpRequest _
Pomocí objektu XMLHttpRequest můžete aktualizovat části webové stránky, aniž byste museli znovu načítat celou stránku.
Zkuste si to sami - příklady
Vytvořte jednoduchý XMLHttpRequest a načtěte data ze souboru TXT.
Načtení informací záhlaví zdroje (souboru).
Načtení konkrétních informací záhlaví zdroje (souboru).
Jak může webová stránka komunikovat s webovým serverem, zatímco uživatel zadává znaky do vstupního pole.
Jak může webová stránka načíst informace z databáze pomocí objektu XMLHttpRequest.
Vytvořte požadavek XMLHttpRequest pro načtení dat ze souboru XML a zobrazení dat v tabulce HTML.
Objekt XMLHttpRequest
Objekt XMLHttpRequest se používá k výměně dat se serverem v zákulisí.
Objekt XMLHttpRequest je snem vývojářů , protože můžete:
- Aktualizujte webovou stránku bez opětovného načítání stránky
- Vyžádejte si data ze serveru po načtení stránky
- Přijímat data ze serveru po načtení stránky
- Odesílejte data na server na pozadí
Metody objektu XMLHttpRequest
Method | Description |
---|---|
abort() | Cancels the current request |
getAllResponseHeaders() | Returns header information |
getResponseHeader() | Returns specific header information |
open(method,url,async,uname,pswd) | Specifies the type of request, the URL, if the request should be handled asynchronously or not, and other optional attributes of a request
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) | send(string) Sends the request off to the server. string: Only used for POST requests |
setRequestHeader() | Adds a label/value pair to the header to be sent |
Vlastnosti objektu XMLHttpRequest
Property | Description |
---|---|
onreadystatechange | Stores a function (or the name of a function) to be called automatically each time the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. Changes from 0 to 4: 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
responseText | Returns the response data as a string |
responseXML | Returns the response data as XML data |
status | Returns the status-number (e.g. "404" for "Not Found" or "200" for "OK") |
statusText | Returns the status-text (e.g. "Not Found" or "OK") |