Handling Live Web Content in WSH - Part 2 - Making the Connection
(Page 2 of 6 )
Now that we have our data in order, it’s time to send it to the server. We’ll begin by connecting to the XMLHttpRequest ActiveX object with a CreateObject() method call to its namespace. In the last article we used “Microsoft.XMLHTTP” as our namespace. This time we’ll use a newer version that should be installed on every machine with Internet Explorer 5 or higher. If you experience errors, just use the older one.
Set objxmlHTTP = CreateObject(“Msxml2.XMLHTTP”)
Next we open our connection with the XMLHttpRequest object’s Open() method. I’ll provide the syntax again for those who need a refresher.
object.Open(“method”, “URL”[, asyncFlag[, “username”[, “password”]]])
The first parameter is a text string containing the HTTP method that we wish to use, in our case, “POST.” The method should be in all caps. The second parameter is a string containing the form action URL. The third and final attribute we’ll be using is an optional Boolean value that tells the XMLHttpObject whether or not the data should be sent asynchronously.
The final two parameters are the username and password, respectively, which we would use if our connection prompted for authentication. It should be noted that the username and password values will only be sent if requested by the server.
So continuing on, we open our connection as follows:
objxmlHTTP.Open(“POST”, actionURL, True)
objxmlHTTP.setRequestHeader “Content-Type”, _
“application/x-www-form-urlencoded”
objxmlHTTP.Send(strPostData)
We’ve used the XMLHttpRequest object’s setRequestHeader method to tell the server the MIME type of the data we are posting. This can usually be omitted, but is required by some servers. We then use the Send() method to send our POST data string by setting it as the parameter. Syntax for the Send() method is shown below.
object.Send([“data”])
Next: Handling the Response >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer