如何使用 vbscript(同步)调用 Web 服务? [英] How to call web service using vbscript (synchronous)?

查看:43
本文介绍了如何使用 vbscript(同步)调用 Web 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上有很多例子,我使用了其中一个.但它是异步工​​作的,我的意思是它不会等待我调用的函数完成.

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

我在 JavaScript 函数中调用 ProcessSend 函数.它连接到网络服务,并返回响应"变量.但是我的 javascript 函数不等待 ProcessSend 函数结果.我怎样才能让它同步?

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?

推荐答案

给你:

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,false)'<< changed true to false here.
    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

顺便说一句,如果您的其余代码是在 JScript 中的,那么您为什么要在 VBScript 中执行此操作?像这样:

Why are you btw doing this in VBScript, if the rest of your code is in JScript? Like this:

function ProcessSend(){ 
    var oXMLHTTP = ActiveXObject("MSXML2.XMLHTTP.4.0") 
    strEnvelope = "callNo=" + callNo + " & exp=" + exp;
    oXMLHTTP.open("POST", "http://localhost:11883/ServiceCall.asmx/" + posFirm, false);
    oXMLHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oXMLHTTP.send(strEnvelope);
    if(oXMLHTTP.readyState == 4){
        if(oXMLHTTP.responseXML.parseError.errorCode != 0){
                response = oXMLHTTP.responseText & " " & oXMLHTTP.responseXML.parseError.reason;
        }else{
                response = oXMLHTTP.responseXML.getElementsByTagName("string")(0).childNodes(0).text;
        }
    }
}

这篇关于如何使用 vbscript(同步)调用 Web 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆