2009-02-13 6 views
8

En fait il y a beaucoup d'exemples et j'en ai utilisé un. Mais ça marche asynchrone, je veux dire qu'il n'attend pas la fonction que j'ai appelée pour finir.Comment appeler le service web en utilisant vbscript (synchrone)?

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 

J'appelle la fonction ProcessSend dans une fonction javascript. Il se connecte à webservice et renvoie la variable "response". Mais ma fonction javascript n'attend pas le résultat de la fonction ProcessSend. Comment puis-je le rendre synchrone?

+1

vous êtes dans un navigateur ou sur Windows Scripting Hôte? Si vous êtes dans un navigateur, pourquoi utilisez-vous la moitié de JavaScript, la moitié de VBScript? – Tomalak

Répondre

9

Ici, vous allez:

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 

Pourquoi vous btw le faire dans VBScript, si le reste de votre code est en JScript? Comme ceci:

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

merci. Pourquoi est-ce que j'utilise vbscript? En fait je ne sais pas, j'essaie de réviser le code écrit en vbscript. – NetSide

9

Si vous faites des appels synchrones, vous n'avez pas besoin du rappel, et vous pouvez réduire le code dans ceci:

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 
Questions connexes