如何使用vbscript(同步)调用Web服务?

发布于 2024-07-14 00:55:11 字数 1214 浏览 8 评论 0原文

其实例子有很多,我就用过其中之一。 但它是异步工​​作的,我的意思是它不会等待我调用的函数完成。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2024-07-21 00:55:11

在这里

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 中执行此操作呢? 像这样:

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;
        }
    }
}

Here you go:

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

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;
        }
    }
}
新一帅帅 2024-07-21 00:55:11

如果您正在进行同步调用,则不需要回调,并且可以将代码缩小为:

function ProcessSend() 
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
    Set oXMLDoc = CreateObject("MSXML2.DOMDocument")

    strEnvelope = "callNo=" & callNo & "&exp=" & exp

    call oXMLHTTP.open("POST", "http://localhost:11883/ServiceCall.asmx/"&posFirm, false)
    call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    call oXMLHTTP.send(strEnvelope)

    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 Sub

If you're doing synchronous calls, you don't need the callback, and you can shrink the code into this:

function ProcessSend() 
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
    Set oXMLDoc = CreateObject("MSXML2.DOMDocument")

    strEnvelope = "callNo=" & callNo & "&exp=" & exp

    call oXMLHTTP.open("POST", "http://localhost:11883/ServiceCall.asmx/"&posFirm, false)
    call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    call oXMLHTTP.send(strEnvelope)

    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 Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文