ក្រោយពីទទួលបានសំណើររួចហើយ server អាចនឹងធ្វើការឆ្លើយតបមកវិញ ដោយបញ្ជូននូវទិន្នន័យដែលយើងចង់បាន ឬមិនបញ្ជូនដោយហេតុផលណាមួយ។ ហើយនៅពេលដែល server ធ្វើការឆ្លើយតបមកវិញ ហេតុការណ៍ឈ្មោះ xhttp.onreadystatechange នឹងកើតមានឡើង ដែលយើងអាចភ្ជាប់វាទៅនឹងក្បួនវិធីឆ្លើយតបហេតុការណ៍ណាមួយបាន។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { alert(this.responseText); } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send();
The onreadystatechange Property
Property | Description |
---|---|
onreadystatechange | Defines a function to be called when the readyState property changes |
readyState |
Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
status |
200: "OK" 403: "Forbidden" 404: "Page not found" For a complete list go to the Http Messages Reference |
statusText | Returns the status-text (e.g. "OK" or "Not Found") |