T086ѧϰ�� | վ��ѧԺ | �����ĵ� | ���� | Ъ���� | IP��ַ��ѯ | �������� | ���ջ��� | �����ֵ� | ���ù��� | ������Ϣ��ѯ

XMLHTTPRequest�����Ժͷ�������

�� ��ɫ�������ߣ�yaohaixiao ����ʱ�䣺2008-11-04 | ���壺�� �� С��
[����]���������ڹ�˾����������׼�ľ�̬ҳ�棬Ϊ����ǿ�ͻ����飬���Ծ���Ҫ��ЩAJAXЧ����Ҳѧ��Ҳ����һ���ڣ�ѧϰAJAX��������AJAXʱʹ�õ�һ����Ҫ�ļ��������ߣ�����XMLHTTPRequest�����ˡ����ﺣХ����ѧϰXMLHTTPR...

���������ڹ�˾����������׼�ľ�̬ҳ�棬Ϊ����ǿ�ͻ����飬���Ծ���Ҫ��ЩAJAXЧ����Ҳѧ��Ҳ����һ���ڣ�ѧϰAJAX��������AJAXʱʹ�õ�һ����Ҫ�ļ��������ߣ�����XMLHTTPRequest�����ˡ����ﺣХ����ѧϰXMLHTTPRequest������һ�������ó���������һ�����������е����϶��Ǻ�Х��ѧϰʱ�������ռ��ģ��������������Ǿ��ټ�����ӡ���ɣ��������������ַ������İ�Ȩ������ϵ��Х����haixiao_yao[at]yahoo.com.cn����

1��XMLHTTPRequest����ʲô����

��ͨ�õĶ���Ϊ��XmlHttp��һ�׿�����javascript��VbScript��Jscript�Ƚű�������ͨ��httpЭ�鴫�ͻ��ӽ���XML���������ݵ�һ��API��XmlHttp�������ô��ǿ��Ը�����ҳ�IJ������ݶ�����Ҫˢ������ҳ�档��������������AJAX��һ���ص�֮һ������

����MSDN�Ľ��ͣ�XmlHttp�ṩ�ͻ���ͬhttp������ͨѶ��Э�顣�ͻ��˿���ͨ��XmlHttp����(MSXML2.XMLHTTP.3.0)��http����������������ʹ��΢��XML�ĵ�����ģ��Microsoft? XML Document Object Model (DOM)������Ӧ��

����˵Щ���⻰����ʵ�������������ͳ����ˣ�ֻ����ǰ��������֧�ֲ�����ֻ��IE�в�֧�֣����Դ�������WEB����Ա��û����ô�����������������������˺ܴ��ظı䣬Mozilla��Safari��������Ϊ��ʵ�ϵı�׼������������������ʼ֧��XMLHTTPRequest�����ˡ�����������Ҫ�ص�˵������XMLHTTPRequestĿǰ������һ��W3C�ı�׼�������ڲ�ͬ���������ϱ���Ҳ����Щ������

2������XMLHTTPRequest����

���ˣ�˵������������������������ô��������ʹ�ã�������ʹ��XMLHTTPRequest�������������ʹ�����Ӧ֮ǰ�����DZ���Ҫ��javascript����һ��XMLHTTPRequest���󡣣�IE��XMLHTTPRequestʵ��Ϊһ��ActiveX������������������[��Firefox/Safari/Opear]������ʵ��Ϊһ�����ص�javascript���󣩡��������Ǿ�������������ô����javascript���������ɣ�

<script language="javascript" type="text/javascript">
<!--
    var xmlhttp;
    // ����XMLHTTPRequest����
    function createXMLHTTPRequest(){
        if(window.ActiveXObject){ // �ж��Ƿ�֧��ActiveX�ؼ�
            xmlhttp = new ActiveObject("Microsoft.XMLHTTP"); // ͨ��ʵ����ActiveXObject��һ����ʵ��������XMLHTTPRequest����
    }
    else if(window.XMLHTTPRequest){ // �ж��Ƿ���XMLHTTPRequestʵ��Ϊһ������javascript����
        xmlhttp = new XMLHTTPRequest(); // ����XMLHTTPRequest��һ��ʵ��������javascript������
    }
    }
//-->
</script>

3�����Ժͷ���

���ڶ���̫���������ø�ҳ�����оٳ�˵�еķ��������ԣ��Ժ�������ϸ��������Ҫ�DZ���Ҳ��ѧϰ�У���

<html>
<head>
<title>XMLHTTPRequest������˵��DEMO</title>
<script language="javascript" type="text/javascript">
<!--
var xmlhttp;
// ����һ��XMLHTTPRequest����
function createXMLHTTPRequext(){
      if(window.ActiveXObject) {
           xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    else if(window.XMLHTTPRequest){
       xmlhttp = new XMLHTTPRequest();
    }
}
function PostOrder(xmldoc)
{
    createXMLHTTPRequext();
   
    // ������open
    // ����һ���µ�http���󣬲�ָ���������ķ�����URL�Լ���֤��Ϣ
    // �﷨��oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
    // ����
    // bstrMethod
    // http���������磺POST��GET��PUT��PROPFIND����Сд�����С�
    // bstrUrl
    // ������URL��ַ������Ϊ���Ե�ַҲ����Ϊ���Ե�ַ��
    // varAsync[��ѡ]
    // �����ͣ�ָ���������Ƿ�Ϊ�첽��ʽ��Ĭ��Ϊtrue������Ϊ�棬��״̬�ı�ʱ������onreadystatechange����ָ���Ļص�������
    // bstrUser[��ѡ]
    // ������������Ҫ��֤���˴�ָ���û���������δָ��������������Ҫ��֤ʱ���ᵯ����֤���ڡ�
    // bstrPassword[��ѡ]
    // ��֤��Ϣ�е����벿�֣������û���Ϊ�գ�����ֵ�������ԡ�
    
    // ��ע�����ô˷����󣬿��Ե���send�������������������ݡ�
    xmlhttp.Open("get", "http://localhost/example.htm", false);
    // var book = xmlhttp.responseXML.selectSingleNode("//book[@id='bk101']");
    // alert(book.xml);

   
    // ���ԣ�onreadystatechange
    // onreadystatechange��ָ����readyState���Ըı�ʱ���¼���������
    // �﷨��oXMLHttpRequest.onreadystatechange = funcMyHandler;
    // ���µ�������ʾ��XMLHTTPRequest������readyState���Ըı�ʱ����HandleStateChange������
    // �����ݽ������Ϻ���readystate == 4����ҳ���ϵ�һ����ť��������
    // ��ע��������ֻд��ΪW3C�ĵ�����ģ�͵���չ.
    xmlhttp.onreadystatechange= HandleStateChange;
   
    // ������send
    // ����������http�����������ջ�Ӧ
    // �﷨��oXMLHttpRequest.send(varBody);
    // ������varBody ����ͨ�����������͵����ݡ���
    // ��ע���˷�����ͬ�����첽��ʽȡ����open�����е�bAsync����������bAsync == False���˷��������ȴ��������ɻ��߳�ʱʱ�Ż᷵�أ�����bAsync == True���˷������������ء�
    // This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.
    // �������͵�����ΪBSTR������Ӧ������Ϊutf-8, �������ʵ�λ������һ������charset���ĵ�����ͷ��
    // If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
    // �������͵�����ΪXML DOM object������Ӧ��������Ϊ��xml�ĵ��������ı��룬������xml�ĵ���û���������룬��ʹ��Ĭ�ϵ�UTF-8��
    // If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
    xmlhttp.Send(xmldoc);
   
    // ������getAllResponseHeaders
    // ��ȡ��Ӧ������httpͷ
    // �﷨��strValue = oXMLHttpRequest.getAllResponseHeaders();
    // ��ע��ÿ��httpͷ���ƺ�ֵ��ð�ŷָ���� ��������send�������ɺ��ſɵ��ø÷�����
    alert(xmlhttp.getAllResponseHeaders());
    // ������getResponseHeader
    // ����Ӧ��Ϣ�л�ȡָ����httpͷ
    // �﷨��strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);
    // ��ע����send�����ɹ����ſɵ��ø÷������������������ص��ĵ�����Ϊ"text/xml", �����仰
    // xmlhttp.getResponseHeader("Content-Type");�������ַ���"text/xml"������ʹ��getAllResponseHeaders������ȡ������httpͷ��Ϣ��
    alert(xmlhttp.getResponseHeader("Content-Type")); // ����httpͷ�е�Content-Type�У���ǰweb�������İ汾�����ơ�
   
   
    document.frmTest.myButton.disabled = true;
    // ������abort
    // ȡ����ǰ����
    // �﷨��oXMLHttpRequest.abort();
    // ��ע�����ô˷����󣬵�ǰ���󷵻�UNINITIALIZED ״̬��
    // xmlhttp.abort();
   
    // ������setRequestHeader
    // ����ָ��������ij��httpͷ
    // �﷨��oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
    // ������bstrHeader���ַ�����ͷ���ơ���
    //       bstrValue���ַ�����ֵ����
    // ��ע�������Ѿ������Ѵ�����������httpͷ���򸲸�֮���˷���������open���������á�
    // xmlhttp.setRequestHeader(bstrHeader, bstrValue);
}
function HandleStateChange()
{
      // ���ԣ�readyState
    // ����XMLHTTP�����ĵ�ǰ״̬
    // �﷨��lValue = oXMLHttpRequest.readyState;
    // ��ע��������������ֻ����״̬�ó���Ϊ4�����ͱ�ʾ.�������£�
    // 0 (δ��ʼ��) �����ѽ�����������δ��ʼ������δ����open������
    // 1 (��ʼ��) �����ѽ�������δ����send����
    // 2 (��������) send�����ѵ��ã����ǵ�ǰ��״̬��httpͷδ֪
    // 3 (���ݴ�����) �ѽ��ղ������ݣ���Ϊ��Ӧ��httpͷ��ȫ����ʱͨ��responseBody��responseText��ȡ�������ݻ����ִ�����
    // 4 (����) ���ݽ�������,��ʱ����ͨ��ͨ��responseBody��responseText��ȡ�����Ļ�Ӧ����
    if (xmlhttp.readyState == 4){
       document.frmTest.myButton.disabled = false;
      
         // ���ԣ�responseBody
       // ����ijһ��ʽ�ķ�������Ӧ����
       // �﷨��strValue = oXMLHttpRequest.responseBody;
       // ��ע��������������ֻ������unsigned array��ʽ��ʾֱ�Ӵӷ��������ص�δ�������Ķ��������ݡ�
       alert(xmlhttp.responseBody);
      
       // ���ԣ�responseStream
       // ��Ado Stream��������ʽ������Ӧ��Ϣ
       // �﷨��strValue = oXMLHttpRequest.responseStream;
       // ��ע��������������ֻ������Ado Stream��������ʽ������Ӧ��Ϣ��
       alert(xmlhttp.responseStream);
      
         // ���ԣ�responseText
       // ����Ӧ��Ϣ��Ϊ�ַ�������
       // �﷨��strValue = oXMLHttpRequest.responseText;
       // ��ע��������������ֻ��������Ӧ��Ϣ��Ϊ�ַ������ء�XMLHTTP���Խ���Ӧ��Ϣ����ΪUnicode�ַ�����
       // XMLHTTPĬ�Ͻ���Ӧ���ݵı��붨ΪUTF-8���������������ص����ݴ�BOM(byte-order mark)��XMLHTTP��
       // �Խ����κ�UCS-2 (big or little endian)����UCS-4 ���ݡ�ע�⣬�������������ص���xml�ĵ�������
       // �Բ�������xml�ĵ��еı�������������Ҫʹ��responseXML��������        
         alert(xmlhttp.responseText);
        
         // ���ԣ�responseXML
       // ����Ӧ��Ϣ��ʽ��ΪXml Document���󲢷���
       // �﷨��var objDispatch = oXMLHttpRequest.responseXML;
       // ��ע��������������ֻ��������Ӧ��Ϣ��ʽ��ΪXml Document���󲢷��ء�������Ӧ���ݲ�����Ч��XML�ĵ���
       // �����Ա���������XMLDOMParseError������ͨ����������DOMDocument������ȡ������Ϣ��
       alert("Result = " + xmlhttp.responseXML.xml);
      
         // ���ԣ�status
       // ���ص�ǰ������http״̬��
       // �﷨��lValue = oXMLHttpRequest.status;
       // ����ֵ�������α�׼http״̬�룬�������£�
       // 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
         // ��ע�������Σ�������ֻ�������ص�ǰ������http״̬��,�����Խ������ݷ��Ͳ��������Ϻ��ſɻ�ȡ��
       alert(xmlhttp.status);
      
       // ���ԣ�statusText
       // ���ص�ǰ��������Ӧ��״̬
       // �﷨��strValue = oXMLHttpRequest.statusText;
       // ��ע���ַ�����������ֻ������BSTR���ص�ǰ��������Ӧ��״̬,�����Խ������ݷ��Ͳ��������Ϻ��ſɻ�ȡ��
       alert(xmlhttp.statusText);
    }
}
//-->
</script>
</head>
<body>
<form name="frmTest">   
    <input name="myButton" type="button" value="Click Me" onclick="PostOrder('http://localhost/example.htm');">
</form>   
</body>
</html>

  • ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
  • �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
������ѯ Health Check Gulangyu
© 2017 T086ѧϰ�� - T086.com(ԭitlearner.com)
RunTime:7.91ms QueryTime:7