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

时间:2009-02-13 12:50:06

标签: web-services vbscript synchronous xmlhttprequest

实际上有很多例子,我使用过其中一个例子。但它是异步的,我的意思是它没有等待我调用完成的功能。

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函数。它连接到webservice,并返回“response”变量。但我的javascript函数不等待ProcessSend函数结果。 我怎样才能使它同步?

2 个答案:

答案 0 :(得分:9)

你走了:

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

答案 1 :(得分:9)

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

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