Monday, August 3, 2009

Call WebService using Javascript

TO call any webservice in javascript use following code... <script> function CallWebService() { PrintService.PrintCode(OnSuccess); // here i m using PrintServer as webservice to print purpose } function OnSuccess(result) { document.getElementId('result').value=result; } </script> ------------------------------------------------------------ Following is my web service public class PrintService : System.Web.Services.WebService { public static string strMyPrintCode = string.Empty; public static string strOfferCode = string.Empty; public PrintService() { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string PrintCode() { string strCode = GetRandomUniqueAlphaNumericCode(8, false); strMyPrintCode = strCode; return strCode; // return unique and random alphanumeric code } } --------------------------- Notice where i m calling webservice...i m passing one argument when calling webservice method named PrintCode, when call to webservice succeeded, Javascript function OnSuccess will be call. You can also create one more function named OnFailure to notify failure of webservice

No comments:

Post a Comment