Software Technology Tips

If Asp.net Ajax framework is used, web service and WCF service methods are consumed by ScriptManager. For jQuery framework, these services are consumed by following ways.
Example:
Create  Ajax-EnabledWCFService, name it "TimeService.svc".
Put Following code in the TimeService class.
 
 
[OperationContract]
 public string GetTime()
 {
  return "<B>" + "Current Server Time : " + DateTime.Now.ToLocalTime() + "</B>" ;
 }
 
ASPX Page
 
 <script language="javascript" type="text/javascript" src="jquery-1.2.6.js"></script>
     <script type="text/javascript">
         $(document).ready(function() {
         $("#btnTime").mouseover(function(event) {
                 $.ajax({
                     type: "post",
                     url: "TimeService.svc/GetTime",
                     data: "{}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function(msg) {
                     ConsumeService(msg);
 
                     },
                     error: ServiceFailed
                 });
             });
         });
         function ConsumeService(result) {
              $("#sp").html(result.d);
         }
         function ServiceFailed(result) {
             alert(result.status + ' ' + result.statusText);
         }    
  </script>    
 
<input id="btnTime" value="Server Time" type="button"  /> 
 <span id="sp"></span>
 
For WebService create TimeService.asmx.
Put following code in in TimeService Class. 
 
  
    [WebMethod]
    public string GetTime()
    {
              return "<B>" + "Curent Server Time : " + DateTime.Now.ToLocalTime() + "</B>";
    }
 Change URL in jQuery $ajax()  to  url: "TimeService.asmx/GetTime"


Related Tags:

Javascript, jQuery, ASP.NET

Author: Jnana Swain

JavaScript

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

copyright (c) Mindfire Solutions 2007-2014. Login