Calling WebService using jQuery in ASP.Net
This blog post demonstrates jquery call webservice in asp.net web applications. Ajax is an acronym standing for Asynchronous JavaScript and XML and this technology help us to load data from the server without a browser page refresh. You can find the below ajax call example :-
![]() |
jQuery Ajax |
Now you will find the implementation for calling webservice using jqyery ajax with example. and how to make a AJAX call to asp.net WebMethod using jQuery ajax. Using jQuery we can call methods or functions defined in server side. Find the below source code :-
![]() |
ajax call jquery example |
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="scripts/jquery-1.7.2.js"></script>
<title>How to Call Asp.Net WebMethod using jQuery Ajax in Web Application?</title>
<script src="jquery-1.7.2.js"></script>
<script type="text/javascript">
function ShowCurrentDateTime() {
$.ajax({
type: "POST", // Request Type
url: "Default.aspx/GetCurrentDateTime", // Page URL-> and Method Name
data: '{name: "' + document.getElementById("txtUserName").value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess, // Success Method
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div><br /><br />
Your Name:
<asp:TextBox ID="txtUserName" runat="server" Text="there.."></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Date & Time" onclick="ShowCurrentDateTime()" />
<br />
<br />
<span id="spnDateTime"></span>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
// Creating a webmethod to get date and time
[System.Web.Services.WebMethod]
public static string GetCurrentDateTime(string name)
{
return "Hey! " + name + Environment.NewLine + "The Current Date & Time is: "
+ DateTime.Now.ToString();
}
}
Download Code Snippet
I hope you have enjoyed the development tips of calling asp.net webservices using ajax call. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Calling WebService using jQuery in ASP.Net
Reviewed by Ravi Kumar
on
10:17 AM
Rating:

Post Comment
No comments: