asynchronousvbscriptxmlhttprequestserverxmlhttp

VBscript Asynchronous XMLHttp Call


for a project I'm working on I'm attempting to do a Asynchronous XMLHTTP Call. I'm using the following code:

soapmessage = _
"<?xml version='1.0' encoding='utf-8'?>"& vbcrlf& vbcrlf & _
"<soap:Envelope"& vbcrlf & _
" xmlns:xsi="&chr(34)&"http://www.w3.org/2001/XMLSchema-instance"&chr(34)& 
vbcrlf & _
" xmlns:xsd="&chr(34)&"http://www.w3.org/2001/XMLSchema"&chr(34)& vbcrlf & _
" xmlns:soap="&chr(34)&"http://www.w3.org/2003/05/soap-
envelope"&chr(34)&">"& vbcrlf & _
" <soap:Body>"& vbcrlf & _
"<notification>"& vbcrlf & _
"   <action>Action</action>"& vbcrlf & _
"   <objectid>333333</objectid>"& vbcrlf & _
"</notification>"& vbcrlf & _
" </soap:Body>" & vbcrlf & _
" </soap:Envelope>"

strEndpoint = "**********"

Set xmlhttp = CreateObject("MSXML2.SERVERXMLHTTP.6.0")
xmlhttp.open "POST", strEndpoint, True
xmlhttp.OnReadyStateChange = doHttpOnReadyStateChange()
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.Send soapmessage

Function doHttpOnReadyStateChange()
    If xmlhttp.ReadyState = 4 Then
        'do something
    End If
End Function

When I try to execute this I get the following:

test.vbs(19, 1) Microsoft VBScript runtime error: Type mismatch: 'xmlhttp.OnReadyStateChange'

any idea what I could be doing wrong? It's my first time trying an async call so i'm a bit puzzled with the OnReadyStateChange


Solution

  • It needs a function reference which you can get using the GetRef() function.

    xmlhttp.OnReadyStateChange = GetRef("doHttpOnReadyStateChange")
    

    Dirk.R: Would like to add that while this is the fix. Keep in mind that the order of the statements also matters!