Created
May 9, 2011 16:58
-
-
Save benjamine/962875 to your computer and use it in GitHub Desktop.
Call WebService with JSON data using MSXML2.ServerXMLHTTP (Windows Script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cscript webservicetest.js | |
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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