- 浏览: 245216 次
文章分类
- 全部博客 (395)
- Tech (0)
- [随笔分类]心情 (95)
- [随笔分类]技术 (112)
- [随笔分类]管理心得 (13)
- [随笔分类]Code SOP (5)
- [随笔分类]望图知意 (11)
- [网站分类]1.首页原创精华.NET区(包含架构设计、设计模式)(对首页文章的要求:原创、高质量、经过认真思考并精心写作) (8)
- [随笔分类]重构代码 (1)
- [随笔分类]童童 (2)
- Program (1)
- [随笔分类]看你知道不知道 (1)
- [网站分类]4.其他技术区 (31)
- [网站分类]3.非技术区(技术之外的文章,但不要涉及任何政治内容) (21)
- [网站分类]9.求职招聘区(个人求职、企业招聘) (0)
- [随笔分类]昨日关注 (15)
- [网站分类]6.读书区(技术书籍阅读心得、书籍推荐) (3)
- [随笔分类]一步一个脚印 (2)
- [网站分类]网站管理区(网站管理方面的疑问、建议、意见, 寻求管理员帮助) (1)
- [网站分类]2..NET新手区(用于发表不合适发表在首页的.NET技术文章,包括小经验、小技巧) (3)
最新评论
昨天晚上在宿舍测试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/
发表评论
-
启动NDuiker项目
2005-01-20 17:05 431今天是进驻博客园的第一天,在这里安家真的很不错,十分感谢DuD ... -
NDuiker项目第2天总结
2005-01-21 08:53 627昨天晚上基本上实现了 ... -
NDuiker项目第3天
2005-01-22 22:50 560今天是周六了,原本以为是很轻松的一天,结果只有到了这个时 ... -
NDuiker项目第6天
2005-01-25 16:50 550这几天好忙,也好累呀,这几天是公司项目收尾的阶段,忙的都透不过 ... -
对.Net 类库的一点思索
2005-02-02 09:29 579这些天主要在测试GDI+的 ... -
测试一个网站的想法
2005-02-05 17:07 525由于项目的需要,测试了一下“上海研发公共服务平台” 登录:ht ... -
项目建议书编写总结
2005-02-25 15:46 745昨天完成了项目建议书的编写工作,整个春节期间的工作到今天全部完 ... -
MyIE 增加了RSS功能
2005-02-28 09:54 545今天更新了MyIE,发现MyIE增加了RSS功能,试用了一下, ... -
www.beihua.edu.cn计划摘录
2005-02-28 14:11 756www.beihua.edu.cn工作计划 1:网站后期制作计 ... -
网站调研资料记录
2005-03-01 15:56 603资料整理备忘 1:内外网IP确认 ... -
静静的看书
2005-03-08 17:18 547这几天还是很忙,但是似乎好了很多,因为目前主要是写一个基于Sm ... -
我的Smart Client 的学习笔记
2005-03-09 15:35 1652User Interface Process (UIP) Ap ... -
研发、开发、运营
2005-03-25 12:15 614这些天很忙,网络也不争气,才刚刚好一点。 ... -
当需求变更来临
2005-04-30 14:26 516昨晚已经和老婆安排好5 ... -
重新登录Window2003的域
2005-05-12 09:58 663今天公司要求重新登录Windows2003的域,记录一下,以后 ... -
当不愿意写字得时候
2005-05-16 15:36 675做项目得前期分析得时候,我还是比较喜欢在本子上写写、画画得,通 ... -
远离技术的时候
2005-06-20 11:05 397这一个月一直在忙一个项目,现在项目的可研报告也 ... -
Asp.Net 中使用客户端Activex控件需要注意的事情
2005-08-13 12:57 924案例:Asp.Net +VB制作的Activex控件 操作系统 ... -
Office开发中的测试的与众不同之处
2005-08-22 14:29 676今天修改了一下自己以 ... -
大家确实都很忙
2005-08-27 08:20 706先谈几个技术问题: 1 ...
相关推荐
NULL 博文链接:https://helius1214.iteye.com/blog/633807
AJAX – onreadystatechange 事件 onreadystatechange 事件 当请求被发送到服务器时,我们需要执行一些基于响应的任务。 每当 readyState 改变时,就会触发 onreadystatechange 事件。 readyState 属性存有 ...
主要介绍了Ajax如何给XMLHttpReq.onreadystatechange =函数传递参数,需要的朋友可以参考下
WEB开发 之 AJAX - onreadystatechange 事件.docx
当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了onreadyStateChange事件实现这一功能。这类似于回调函数的做法。onreadyStateChange事件可...
firefox不支持iframe的onreadystatechange事件,不过有个方法可以实现同等的效果,需要的朋友可以参考下
各浏览器对link标签onload/onreadystatechange事件支持的差异分析,需要的朋友可以参考下。
IE9/10同时支持script元素的onload和onreadystatechange事件</title> [removed][removed] </head> <body> </body> </html> 结果: IE6/7/8 : 弹出2 IE9/10 : 弹出2,1 F
firefox下对ajax的onreadystatechange的支持分析。用的到的朋友可以参考下。
大文件下载不再卡机。XMLHTTP的OnReadyStateChange事件进行异步下载的演示。例子是下载一个8M的mp3文件
AJAX和jQuery(达内课程)
3.2,用onreadystatechange来指定数据装载的方式.(onreadystatechange是一个异步响应事件,就是因为它,ajax才那么完美.onreadystatechange事件中可以接收到服务器传回来的数据,通过分析和利用这些数据从而完成...
No need to use IE's silly XDomainRequest Object Easy XHR access to file servers: Amazon Dropbox Includes XHook and its features proxy.html files (slaves) may: White-list domains White-...