如何使用vbscript(同步)调用Web服务?
其实例子有很多,我就用过其中之一。 但它是异步工作的,我的意思是它不会等待我调用的函数完成。
function ProcessSend()
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLHTTP.onreadystatechange = getRef("HandleStateChange")
strEnvelope = "callNo="&callNo&"&exp="&exp
call oXMLHTTP.open("POST","http://localhost:11883/ServiceCall.asmx/"&posFirm,true)
call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
call oXMLHTTP.send(strEnvelope)
end function
Sub HandleStateChange
if(oXMLHTTP.readyState = 4) then
dim szResponse: szResponse = oXMLHTTP.responseText
call oXMLDoc.loadXML(szResponse)
if(oXMLDoc.parseError.errorCode <> 0) then
'call msgbox("ERROR")
response = oXMLHTTP.responseText&" "&oXMLDoc.parseError.reason
'call msgbox(oXMLDoc.parseError.reason)
else
response = oXMLDoc.getElementsByTagName("string")(0).childNodes(0).text
end if
end if
End Sub
我在 JavaScript 函数中调用 ProcessSend 函数。 它连接到网络服务,并返回“响应”变量。 但我的 javascript 函数不等待 ProcessSend 函数结果。 我怎样才能使它同步?
Actually there many examples and I have used one of them. But it works asynchronous, I mean it is not waiting the function that I called to finish.
function ProcessSend()
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
Set oXMLDoc = CreateObject("MSXML2.DOMDocument")
oXMLHTTP.onreadystatechange = getRef("HandleStateChange")
strEnvelope = "callNo="&callNo&"&exp="&exp
call oXMLHTTP.open("POST","http://localhost:11883/ServiceCall.asmx/"&posFirm,true)
call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
call oXMLHTTP.send(strEnvelope)
end function
Sub HandleStateChange
if(oXMLHTTP.readyState = 4) then
dim szResponse: szResponse = oXMLHTTP.responseText
call oXMLDoc.loadXML(szResponse)
if(oXMLDoc.parseError.errorCode <> 0) then
'call msgbox("ERROR")
response = oXMLHTTP.responseText&" "&oXMLDoc.parseError.reason
'call msgbox(oXMLDoc.parseError.reason)
else
response = oXMLDoc.getElementsByTagName("string")(0).childNodes(0).text
end if
end if
End Sub
I call ProcessSend function in a javascript function. It connects to webservice, and returns the "response" variable. But my javascript function do not wait ProcessSend function result.
How can I make it synchronous?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里
,如果您的其余代码是用 JScript 编写的,那么您为什么要在 VBScript 中执行此操作呢? 像这样:
Here you go:
Why are you btw doing this in VBScript, if the rest of your code is in JScript? Like this:
如果您正在进行同步调用,则不需要回调,并且可以将代码缩小为:
If you're doing synchronous calls, you don't need the callback, and you can shrink the code into this: