Visit Map

Wednesday, November 05, 2008

XMLHTTPRequest Object accessing web service

Hi
I just browing thorugh the xmlhttpobject and trying to call web service.I am posting the code the here. It calls the holiday web service GetHolidaysAvailable method which calls the method Synchronously.
The following is code


function GetSynchronousJSONResponse() {
var xmlhttp = null;
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) {
if (new ActiveXObject("Microsoft.XMLHTTP"))
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
// to be ensure non-cached version of response
//url = url + "?rnd=" + Math.random();
url="http://www.holidaywebservice.com/Holidays/US/USHolidayService.asmx/GetHolidaysAvailable";
// xmlhttp.setRequestHeader("SOAPAction", "http://www.holidaywebservice.com/Holidays/US/USHolidayService.asmx/GetHolidaysAvailable");
xmlhttp.open("POST", url, false);//false means synchronous




//xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlhttp.send();
//xmlhttp.send("action="+"null");
var responseText = xmlhttp.responseText;
alert(responseText);
return responseText;
}

This is one of learnings with XMLHTTPRequest object. Hope you find it useful