I tested VB6 in the dormitory last night to call XMLHTTP for communication. synchronous calling is still very easy, but asynchronous calling is confusing, mainly because XMLHttpRequest. the onreadystatechange attribute is unclear about how to use it. It is easy to directly use the callback function in Javascript, but it is very confusing in VB. Later, let's look at an article in msdn 2003.ArticleIt is not difficult to understand, that is, you do not know.
The following content is copied from msdn. Note the yellow content.
This article shows how to use Dom, timer, and packaging class for asynchronous calling. I personally feel that the best way is the packaging class method, which is more elaborate. The key is that it has never been used before.
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, quota 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 commandementclick ()
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 shocould 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 varous places to step through the code.
Link: http://www.nedcomp.nl/support/origdocs/xml4/extracted/dom_howdoi_0rj7.aspx
Reference: http://www.xmlhttp.cn/