实际上有很多例子,我使用过其中一个例子.但它是异步的,我的意思是它不等待我打电话完成的功能.
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函数的结果.如何让它同步?
干得好:
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; } } }
如果您正在进行同步调用,则不需要回调,并且可以将代码缩小为:
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