3

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

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

12 Comments

Tried it with the GetRef("") arround the functionname. No more error but also nothing happens.. Put some debug code in there to get a msgbox or anything but never seems to enter the function (even outside the "if")..
@DirkR. strange, have you tried a Sub procedure instead of a Function as your not returning anything, can't see it making a difference though. It's almost like the OnReadyStateChange event isn't firing.
Just tried with a Sub, but same outcome.. It does seem like it's not fireing. I tried using a regular MSXML2.XMLHTTP.6.0 object instead of the server one but also nothing.. really stumped here!
@DirkR. because it's ASync the Send() method will return straight away, ending the script, so the next callback never has chance to run. You'll need to use WScript.Sleep() or something to keep the script running.
Well you fixed the original question! VBScript just has a strange implementation of the SERVERXMLHTTP object i guess that supports async but not really. Thanks for the assistance!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.