首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>Xml编程>资讯:XMLHTTPRequest属性及方法详解教程

XMLHTTPRequest属性及方法详解教程

www.jz123.cn  2008-10-25   来源:   中国建站    编辑整理    我要投递新闻

// 如下的例子演示当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头名称和值用冒号分割,并以rn结束。当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
    // 单独指定请求的某个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

上一篇:XML文档的基本操作 下一篇:XML入门的常见问题(四)

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图