1.onreadystatechange 属性
onreadystatechange 属性存有处理服务器响应的函数。
xmlHttp.onreadystatechange=function()
{
// 我们需要在这里写一些代码
}
2.readyState 属性
readyState 属性存有服务器响应的状态信息。每当 readyState 改变时,onreadystatechange 函数就会被执行。
这是 readyState 属性可能的值:
|
状态 |
描述 |
|
0 |
请求未初始化(在调用 open() 之前) |
|
1 |
请求已提出(调用 send() 之前) |
|
2 |
请求已发送(这里通常可以从响应得到内容头部) |
|
3 |
请求处理中(响应中通常有部分数据可用,但是服务器还没有完成响应) |
|
4 |
请求已完成(可以访问服务器响应并使用它) |
我们要向这个 onreadystatechange 函数添加一条 If 语句,来测试我们的响应是否已完成(意味着可获得数据):
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)//表示请求完成
{
// 从服务器的response获得数据
}
}
3.status属性
|
Number |
Description |
|
100 |
Continue |
|
101 |
Switching protocols |
|
200 |
OK (发送成功) |
|
201 |
Created |
|
202 |
Accepted (公认的) |
|
203 |
Non-Authoritative Information |
|
204 |
No Content |
|
205 |
Reset Content |
|
206 |
Partial Content |
|
300 |
Multiple Choices(权限) |
|
301 |
Moved Permanently |
|
302 |
Found |
|
303 |
See Other |
|
304 |
Not Modified |
|
305 |
Use Proxy |
|
307 |
Temporary Redirect |
|
400 |
Bad Request (重定向) |
|
401 |
Unauthorized |
|
402 |
Payment Required |
|
403 |
Forbidden |
|
404 |
Not Found |
|
405 |
Method Not Allowed |
|
406 |
Not Acceptable |
|
407 |
Proxy Authentication Required |
|
408 |
Request Timeout |
|
409 |
Conflict |
|
410 |
Gone |
|
411 |
Length Required |
|
412 |
Precondition Failed |
|
413 |
Request Entity Too Large |
|
414 |
Request-URI Too Long |
|
415 |
Unsupported Media Type |
|
416 |
Requested Range Not Suitable |
|
417 |
Expectation Failed |
|
500 |
Internal Server Error |
|
501 |
Not Implemented |
|
502 |
Bad Gateway |
|
503 |
Service Unavailable |
|
504 |
Gateway Timeout |
|
505 |
HTTP Version Not Supported |
if(xmlHttp.readyState==4)//表示请求完成
{
// 从服务器的response获得数据
If(xmlHttp.status==200)
{
//可要可不要,等于200表示发送成功
}
}
4. responseText 属性
可以通过 responseText 属性来取回由服务器返回的数据。
在我们的代码中,我们将把时间文本框的值设置为等于 responseText:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}