.

using JQuery with a Asp.Net WebService

Here follows the web service code and html code (+Javascript / JQuery) to make the necessary calls.
Please note the [System.Web.Script.Services.ScriptService] code that needs to be added to the default webservice class.

save the following as: GetTimeWebService.cs (in the “app_code” folder) :

 

 


 

using
System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class GetTimeWebService : System.Web.Services.WebService {

    public GetTimeWebService () {    }


    [WebMethod]

   public string checkit()

   {

        return "Hello World: " + DateTime.Now.ToLongTimeString();

    }

}

 

 

and this is the HTML page .. name whatever you want to:

 

 

<div>the time is: <span id="time_container"></span></div>

<script> //if you create the webservice in the same file then use ... url: "

<%=HttpContext.Current.Request.Url.LocalPath%>/GetTime", function UpdateTime()
{
$.ajax({
type: "POST",
url: "GetTimeWebService.asmx/checkit",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg)
{ $('#time_container').text(msg.d); } ,
error: function()
{ $('#time_container').text('lost connection'); }
}
);}
UpdateTime(); setInterval(UpdateTime,1000);
</script>

 

What's your thoughts on this?

*

Protected by WP Anti Spam