当前位置:  开发笔记 > 编程语言 > 正文

如何使用vbscript(同步)调用Web服务?

如何解决《如何使用vbscript(同步)调用Web服务?》经验,为你挑选了2个好方法。

实际上有很多例子,我使用过其中一个例子.但它是异步的,我的意思是它不等待我打电话完成的功能.

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,并返回"response"变量.但是我的javascript函数不会等待ProcessSend函数的结果.如何让它同步?



1> svinto..:

干得好:

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



2> Manne..:

如果您正在进行同步调用,则不需要回调,并且可以将代码缩小为:

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

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有