2009-02-13 18 views
8

Eigentlich gibt es viele Beispiele und ich habe eines davon benutzt. Aber es funktioniert asynchron, ich meine, es wartet nicht auf die Funktion, die ich aufgerufen habe, um zu beenden.Wie Web-Service mit VBScript (synchron) aufrufen?

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 

Ich rufe ProcessSend-Funktion in einer JavaScript-Funktion. Es verbindet sich mit dem Webservice und gibt die Variable "response" zurück. Aber meine Javascript-Funktion wartet nicht auf das Ergebnis der ProcessSend-Funktion. Wie kann ich es synchron machen?

+1

Sind Sie in einem Browser oder auf Windows Scripting Gastgeber? Wenn Sie in einem Browser sind, warum verwenden Sie halb JavaScript, halb VBScript? – Tomalak

Antwort

9

Hier gehen Sie:

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 

Warum sind Sie btw dies in VBScript tun, wenn der Rest des Codes in JScript ist? Wie folgt aus:

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

danke. Warum verwende ich VBScript? Eigentlich weiß ich nicht, ich versuche, den in VBScript geschriebenen Code zu überarbeiten. – NetSide

9

Wenn Sie synchrone Anrufe tun, müssen Sie den Rückruf nicht benötigen, und Sie können den Code in diese schrumpfen:

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 
Verwandte Themen