最近听了博客园的ajax课程做点笔记。
主要的函数:
XMLHttpRequest对象(Cont.)
􀂄方法:
􀂅void open(Stringmethod, String url, Boolean async)
􀂅void send(Stringbody)
􀂅void setHeader(Stringheader, String value)
􀂅String getResponseHeader(Stringheader)
􀂅String getAllResponseHeaders()
􀂅void abort()
XMLHttpRequest对象(Cont.)
􀂄属性:
􀂅Number readyState
􀂅Function onreadystatechange
􀂅String responseText
􀂅XmlDocumentresponseXML
􀂅Number status
􀂅String statusText
跨浏览器使用AJAX
<script language="javascript">
        
var xmlHttpRequest = null;
        
        
function getXMLHttpRequest()
        
{
            
if(window.XMLHttpRequest)
            
{
                
return new XMLHttpRequest();
            }

            
else if (window.ActiveXObject)
            
{
                request 
= new ActiveXObject("Microsoft.XMLHTTP");
                
                
if (!request)
                
{
                    request 
= new ActiveXObject("Msxml2.XMLHTTP");
                }

                
                
return request;
            }


        }

        
        
function selectionChanged()
        
{
            
var url = "TextHandler.ashx?index=" + document.getElementById("select").value;
            
            xmlHttpRequest 
= getXMLHttpRequest();
            xmlHttpRequest.onreadystatechange 
= onReadyStateChange;
            xmlHttpRequest.open(
"GET", url, true);
            xmlHttpRequest.send(
null);
        }

        
        
function onReadyStateChange()
        
{
            
if (xmlHttpRequest.readyState == 4)
            
{
                
if (xmlHttpRequest.status == 200)
                
{
                    document.getElementById(
"text").innerHTML = 
                        xmlHttpRequest.responseXML.documentElement.firstChild.nodeValue;
                }

            }

        }

    
</script>

错误处理
􀂄open时抛出错误
􀂅非法URL
􀂅跨域名URL
􀂄访问status时错误(没有得到正确Header)
􀂅服务器完全无法响应
􀂅URL指向不存在
􀂄status错误(不是2xx)
􀂅503 Service Unavailable
􀂅404 Not Found
<script language="javascript">
        
var xmlHttpRequest = null;
        
        
function getXMLHttpRequest()
        
{
            
if(window.XMLHttpRequest)
            
{
                
return new XMLHttpRequest();
            }

            
else if (window.ActiveXObject)
            
{
                request 
= new ActiveXObject("Microsoft.XMLHTTP");
                
                
if (!request)
                
{
                    request 
= new ActiveXObject("Msxml2.XMLHTTP");
                }

                
                
return request;
            }


        }

        
        
function start()
        
{
            xmlHttpRequest 
= getXMLHttpRequest();
            
try
            
{
                xmlHttpRequest.onreadystatechange 
= onReadyStateChange;
                xmlHttpRequest.open(
"GET", url, true);
                xmlHttpRequest.send(
null);
            }

            
catch(e)
            
{
                
// invalid url
                // cross browser data access, etc.
            }

        }

        
        
function onReadyStateChange()
        
{
            
try
            
{
                
if (xmlHttpRequest.readyState == 4)
                
{
                    
if (xmlHttpRequest.status == 200)
                    
{
                        
// do sth.
                    }

                    
else
                    
{
                        
// 503 Service Unavailable
                        // 404 Not Found, etc.
                    }

                }

            }

            
catch(e)
            
{
                
// the server host is completely down
                // the url is not correct, etc.
            }

        }

    
</script>
/Files/cmzzlh/ajax-lesson1-demo.zip
posted on 2006-11-09 14:50  公木子  阅读(975)  评论(1编辑  收藏  举报