Skip to content

Instantly share code, notes, and snippets.

@benjamine
Created May 9, 2011 16:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benjamine/962875 to your computer and use it in GitHub Desktop.
Save benjamine/962875 to your computer and use it in GitHub Desktop.
Call WebService with JSON data using MSXML2.ServerXMLHTTP (Windows Script)
Cscript webservicetest.js
pause
processSend();
function processSend(attempts) {
var xmlhttp = new ActiveXObject("MSXML2.ServerXMLHTTP");
var data = '{"prefixText":"iron","count":5,"contextKey":"Nw,ct,en"}';
var svcurl = "http://localhost/website/services/itemtablewebsvc.asmx";
var svcmethod = "GetAutoCompleteItems";
xmlhttp.open("POST", svcurl + "/" + svcmethod, false);
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
dataReceived(xmlhttp);
}
};
xmlhttp.setTimeouts(5000, 60000, 10000, 10000);
try {
xmlhttp.send(data);
} catch (err) {
WScript.Echo("Error:" + err.description + "\n");
if (!attempts || attempts < 5) {
WScript.Echo("Retry " + ((attempts || 0) + 1) + "...");
processSend((attempts || 0) + 1);
} else {
WScript.Echo("Too many attemtps.");
}
}
}
function dataReceived(xmlhttp) {
var response;
if (xmlhttp.responseXML.parseError.errorCode != 0) {
response = xmlhttp.responseText & " " & xmlhttp.responseXML.parseError.reason;
WScript.Echo("Response: " + response);
} else {
// response = xmlhttp.responseXML.getElementsByTagName("string")(0).childNodes(0).text;
// response = xmlhttp.responseXML;
response = xmlhttp.responseText;
WScript.Echo("Response:\n" + response + "\n");
var data = eval("(" + xmlhttp.responseText + ")");
WScript.Echo("Data:");
for (var i = 0; i < data.d.length; i++) {
WScript.Echo(i + ":" + eval("(" + data.d[i] + ")").First);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment