2009-02-13 34 views
8

Thực tế có nhiều ví dụ và tôi đã sử dụng một trong số chúng. Nhưng nó hoạt động không đồng bộ, ý tôi là nó không đợi hàm mà tôi gọi để kết thúc.Cách gọi dịch vụ web bằng cách sử dụng vbscript (đồng bộ)?

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 

Tôi gọi hàm ProcessSend trong hàm javascript. Nó kết nối với webservice và trả về biến "response". Nhưng hàm javascript của tôi không chờ kết quả hàm ProcessSend. Tôi làm cách nào để đồng bộ hóa?

+1

Bạn có đang ở một trình duyệt hoặc trên Windows Scripting Host? Nếu bạn đang ở trong một trình duyệt, tại sao bạn sử dụng một nửa JavaScript, một nửa VBScript? – Tomalak

Trả lời

9

Ở đây bạn đi:

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 

Tại sao các bạn btw làm điều này trong VBScript, nếu phần còn lại của mã của bạn là trong JScript? Như thế này:

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

cảm ơn. Tại sao tôi sử dụng VBScript? Trên thực tế tôi không biết, tôi đang cố gắng sửa đổi mã được viết bằng vbscript. – NetSide

9

Nếu bạn đang làm cuộc gọi đồng bộ, bạn không cần phải gọi lại, và bạn có thể thu nhỏ mã vào đây:

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 
Các vấn đề liên quan