2009-02-13 69 views
8

其实有很多例子,我用过其中之一。但它是异步的,我的意思是它不会等待我调用完成的函数。如何使用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函数。它连接到webservice,并返回“响应”变量。但是我的javascript函数不会等待ProcessSend函数的结果。 我如何使它同步?

+1

你在浏览器中或在Windows脚本主办?如果你在浏览器中,为什么你使用一半的JavaScript,一半的VBScript? – Tomalak 2009-02-13 12:58:48

回答

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 

你为什么要顺便说一句这样做在VBScript中,如果你的代码的其余部分是在JScript?就像这样:

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

谢谢。为什么我使用VBScript?其实我不知道,我正试图修改用vbscript编写的代码。 – NetSide 2009-02-13 13:08:14

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