Here Comes the WebService.
Dont forget to add the Namespace: System.Web.Script.Services. (Comes with AJAX Extensions).
(You may face problem while making the server call if the Website you built is not an Ajax Enabled Website.)
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ThisService : System.Web.Services.WebService
{
public ThisService()
{
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string dlaID)
{
return "Hello World";
}
}
Now the .aspx page with Markup.
Download the latest JQuery file and add to your project and refer.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JQServiceCall.aspx.cs" Inherits="JQServiceCall" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>JQuery ASP.Net Page</title>
<script type="text/javascript" src="Scripts/jquery-1.4.2.min.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(
function() {
var ID = '49214';
$.ajax({
type:"POST",
url:"ThisService.asmx/HelloWorld",
dataType:"json",
// data: "{}",
data:'{"dlaID":' + ID + '}',
contentType:"application/json; charset=utf-8",
success:function(msg) {
if (msg != null) {alert(msg);}
else {alert('My Message!');
}},
error:
function(xhr) {if (xhr != null) {alert("Error occured \n" + xhr.responseText);}}
});});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<font color="#000000">User Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<span id="status"></span></font>
<div>
</div>
</form>
</body>
</html>
1 comment:
It is useful and easy to understand.
Thanks for sharing this code.
Post a Comment