`

Use the onReadyStateChange Property (Visual Basic)

阅读更多

昨天晚上在宿舍测试VB6调用XMLHttp进行通信,其中同步调用还是很容易的,但是异步调用很是迷惑,主要是因为XMLHttpRequest.OnReadyStateChange 属性不清楚怎么用,如果在javascript里边直接用回调函数还很容易,但是在VB里边就很迷惑了,后来看MSDN 2003中的一篇文章才搞清楚,其实也不难,就是你不知道。

下边的内容是从MSDN上照抄的,其中打黄色的地方是需要注意的。

这篇文章给出了利用DOM、Timer和包装类的方式进行异步调用,个人感觉最爽的还是包装类的方式,比较的精巧,关键是以前没有这么用过。

 

Microsoft XML Core Services (MSXML) 4.0 - DOM Developer's Guide 

Use the onReadyStateChange Property (Visual Basic)
This topic discusses the implementation details necessary to use onreadystatechange notification in Microsoft Visual Basic applications.

Background Information for onReadyStateChange Events
The onreadystatechange callback function was not implemented as a COM automation event in the IXMLHTTPRequest and IServerXMLHTTPRequest components. This is because these components are heavily used in scripting environments, many of which do not support COM events. The onreadystatechange callback function was intended to be easy to use when working with scripting clients such as VBScript and JScript.

Because the onreadystatechange property was not implemented through COM-based automation events, Visual Basic (and C/C++) applications need to implement this callback functionality differently.

Using onReadyStateChange in Visual Basic Applications
In Visual Basic, you can use any of the following approaches to design applications that support onreadystatechange events.

Use a timer control to poll the readyState property. When the value of the readyState property indicates that the data is ready, turn the timer off.
Use a DomDocument object to load the XML and handle the state using the WithEvents keyword.
Note   If you are using the IXMLHTTPRequest and IServerXMLHTTPRequest components to first post your XML data to a Web server, this option will not work for you.
Create a wrapper class, and create a procedure to handle the event within the class module. Set the procedure to the default, and bind the class to the onreadystatechange event to either the IXMLHTTPRequest or IServerXMLHTTPRequest component, depending on which component you are using with your application.
The following sample application demonstrates each of these three approaches.

To use OnReadyStateChange in a Visual Basic application

Open Microsoft? Visual Basic? 6.0. In the New Project dialog box, double-click Standard EXE.
On the Project menu, click References.
In the Available References list, select Microsoft XML,v4.0, and then click OK.
Add four command buttons to Form1 and set the caption of each button as follows:

Control Caption
Command1 Fail
Command2 Polling using Timer
Command3 Using Class Wrapper
Command4 Using DOMDocument


Add a timer control to Form1.
Copy and paste the following code into Form1.

Option Explicit

Public XMLHttpRequest As MSXML2.XMLHTTP40
Public WithEvents XMLDom As MSXML2.DOMDocument40

Private Function FunctionReadyStateChange()
    Debug.Print XMLHttpRequest.readyState
End Function

Private Sub Command1_Click()
    FailedOnReadyState
End Sub

Private Sub Command2_Click()
    TimerResolution
End Sub

Private Sub Command3_Click()
    ClassResolution
End Sub

Private Sub Command4_Click()
    DOMResolution
End Sub

Private Sub FailedOnReadyState()
On Error GoTo FailedState
    If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
   
    Set XMLHttpRequest = New MSXML2.XMLHTTP40
   
    ' Assign the wrapper class object to onreadystatechange.
    XMLHttpRequest.OnReadyStateChange = FunctionReadyStateChange

    ' Get some stuff asynchronously.
    XMLHttpRequest.open "GET", "http://localhost/test.xml", True
    XMLHttpRequest.send
   
    Exit Sub

FailedState:
    MsgBox Err.Number & ": " & Err.Description

End Sub

Private Sub TimerResolution()
    If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
    Timer1.Interval = 1
   
    Set XMLHttpRequest = New MSXML2.XMLHTTP40
   
    ' Get some stuff asynchronously.
    XMLHttpRequest.open "GET", "http://localhost/test.xml", True
    XMLHttpRequest.send
End Sub

Private Sub ClassResolution()
    If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
   
    Dim MyOnReadyStateWrapper As MyReadyStateHandler
    Set XMLHttpRequest = New MSXML2.XMLHTTP40

    ' Create an instance of the wrapper class.
    Set MyOnReadyStateWrapper = New MyReadyStateHandler

    ' Assign the wrapper class object to onreadystatechange. 
    XMLHttpRequest.OnReadyStateChange = MyOnReadyStateWrapper

    ' Get some stuff asynchronously.
    XMLHttpRequest.open "GET", "http://localhost/test.xml", True
    XMLHttpRequest.send

End Sub

Private Sub DOMResolution()
    If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing
    If Not XMLDom Is Nothing Then Set XMLDom = Nothing
   
    Set XMLDom = New MSXML2.DOMDocument40
   
    XMLDom.async = True
    XMLDom.Load "http://localhost/test.xml"
   
End Sub

Private Sub Timer1_Timer()
    Debug.Print XMLHttpRequest.readyState
    If XMLHttpRequest.readyState = 4 Then
        MsgBox "Done"
        Timer1.Interval = 0
    End If
End Sub

Private Sub XMLDom_onreadystatechange()
    Debug.Print XMLDom.readyState
    If XMLDom.readyState = 4 Then
        MsgBox "Done"
    End If
End Sub

From the Project menu, click Add Class Module.
Change the name of the new class module from "Class1" to "MyReadyStateHandler"
Paste the following code into the class module:

Option Explicit

Sub OnReadyStateChange()
    Debug.Print Form1.XMLHttpRequest.readyState
    If Form1.XMLHttpRequest.readyState = 4 Then
        MsgBox "Done"
    End If
End Sub

In the sample code added in the previous step, highlight the procedure name "OnReadyStateChange" by selecting it in the Code window.
From the Tools menu, click Procedure Attributes.
In the Procedure Attributes dialog, the Name combo box should show "OnReadyStateChange."

Click Advanced.
In Procedure ID, select "(Default)" from the available options.
Click OK.

Save the class module (MyReadyStateHandler.cls) to file.
Open Notepad and paste the following XML into it
<?xml version="1.0"?>
<Root>
  <Testing>This is to test the onreadystatechange event on the XMLHTTPRequest or DOMDocument</Testing>
  <Testing>This is due to the event not being declared in the type library</Testing>
</Root>
Save the file as test.xml to your IIS localhost directory. For example, this folder might be C:\Inetpub\wwwroot for a typical default installation of IIS with Windows 2000.
In Visual Basic, from the Run menu, click Start to run the application.
Try the following command options to observe the different approaches to using the onreadystatechange event within Visual Basic.
To force a ready state failure, click Fail.
To view the polling resolution, click Polling using Timer.
To view the wrapper class solution, click Using Class Wrapper.
To view the DOMDocument approach, click Using DomDocument.
For each of the code paths in the previous step, you can place brake-points at various places to step through the code.

原文链接:http://www.nedcomp.nl/support/origdocs/xml4/extracted/dom_howdoi_0rj7.aspx
参考:http://www.xmlhttp.cn/

分享到:
评论

相关推荐

    firefox3中onreadystatechange事件不会触发

    在Firefox 3中,`onreadystatechange`事件是一个常见的JavaScript事件,用于监测XMLHttpRequest对象的状态变化。然而,根据标题所示的问题,这个事件似乎在特定环境下没有按照预期的方式触发。这可能是由于Firefox 3...

    AJAX – onreadystatechange 事件

    在 AJAX 中,`onreadystatechange` 事件扮演了关键角色,它使得开发者能够根据请求的状态来执行相应的处理。 `onreadystatechange` 事件是一个事件处理器,每当 `XMLHttpRequest` 对象的 `readyState` 属性发生变化...

    Ajax 给 XMLHttpReq.onreadystatechange传递参数

    `onreadystatechange`是XMLHttpRequest对象的一个重要事件,当请求的状态改变时,它会被触发。 `XMLHttpRequest.onreadystatechange`属性通常用来设置一个函数,该函数会在每次请求的状态发生改变时执行。状态变化...

    iframe的onreadystatechange事件在firefox下的使用

    `onreadystatechange`事件在Internet Explorer浏览器中被广泛使用,当`iframe`的内容状态发生变化时,例如加载完成,这个事件会被触发。然而,Firefox和其他遵循W3C标准的浏览器并不支持`onreadystatechange`事件,...

    WEB开发 之 AJAX - onreadystatechange 事件.docx

    `onreadystatechange`事件是AJAX中的关键部分,它使得开发者能够在XMLHttpRequest对象的状态发生变化时执行相应的处理程序。 XMLHttpRequest对象是AJAX的核心,它提供了与服务器进行通信的能力。`...

    AJax 学习笔记二(onreadystatechange的作用)

    当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了onreadyStateChange事件实现这一功能。这类似于回调函数的做法。onreadyStateChange事件可...

    各浏览器对link标签onload/onreadystatechange事件支持的差异分析

    在网页开发中,link标签常用于引入CSS样式表,而onload和onreadystatechange事件则用于检测样式表是否已经加载完成。然而,不同的浏览器对这两个事件的支持情况存在差异,这可能会导致在不同浏览器上的表现不一致。...

    vb6用的xmlhttp模块,get和post方式提交数据,异步获取,不卡死,十分好用

    这个标题提到的"vb6用的xmlhttp模块"实际上是指使用VB6(Visual Basic 6)编程时,通过XMLHTTP组件进行GET和POST请求,以实现异步的数据提交和获取,避免程序卡死的情况。这种技术对于创建网络投票程序、信息采集...

    get与post实例

    本文将深入探讨GET和POST的区别、应用场景以及VB(Visual Basic)中如何使用它们。 GET和POST是HTTP请求的核心方法,用于从服务器获取数据或向服务器发送数据。GET主要用于从服务器获取资源,它的参数通常显示在URL...

    VB6Get和POST请求异步处理请求

    在VB6(Visual Basic 6)编程环境中,GET和POST是两种主要的HTTP请求方法,用于从Web服务器获取数据或提交数据。这两种方法在进行网络通信时起着至关重要的作用,尤其是在开发基于Web的应用程序时。然而,在进行同步...

    仅IE9/10同时支持script元素的onload和onreadystatechange事件分析

    IE9/10同时支持script元素的onload和onreadystatechange事件&lt;/title&gt; [removed][removed] &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; 结果: IE6/7/8 : 弹出2 IE9/10 : 弹出2,1 F

    firefox下对ajax的onreadystatechange的支持情况分析

    在Firefox浏览器中,AJAX(异步JavaScript和XML)的`onreadystatechange`事件处理与Internet Explorer(IE)存在一些差异,这些差异可能会导致在处理服务器响应时遇到问题。`onreadystatechange`是AJAX请求的核心...

    use_of_ajax

    4. **处理响应**:在`onreadystatechange`回调中,检查`readyState`和`status`,如果成功则解析返回的数据,根据结果更新UI,例如显示错误提示或预加载用户信息。 ### 3. 示例代码 以下是一个简单的示例,展示了...

    Excel vba使用XMLHTTP进行异步下载的演示

    本篇文章将深入探讨如何使用XMLHTTP进行异步下载,以及`OnReadyStateChange`事件在其中的作用。 首先,XMLHTTP是Microsoft ActiveX控件的一部分,它可以实现HTTP、HTTPS协议的异步通信。在VBA中,我们可以通过创建...

Global site tag (gtag.js) - Google Analytics