8

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

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

4

2 回答 2

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;
        }
    }
}
于 2009-02-13T12:58:09.410 回答
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
于 2009-11-12T13:21:18.253 回答