This example covers how to Call Asp.Net WebService PageMethods Or WebMethod With Parameters From JQuery Or JavaScript.
To Call WebService PageMethod through Ajax ,JavaScript or JQuery we need to uncomment or add following line before writing WebMethod
Add jquery-1.7.2.min.js in application and reference it in Head section of page.
Add and create Web Service by Right click on solution explorer > Add New Item > WebService.asmx.
I have created a WebMethod which takes and return string parameter in it.
C# CODE
VB.NET CODE
Place one text input html control, one input button type on page, you can place asp.net button control either.
Add following Script in Head section of page to call PageMethod using JQuery.
To call WebService using JavaScript, we can write script as follows
Place ScriptManager on the page and add ServiceReference and path in it.
Write this Script in Head of page.
To Call WebService PageMethod through Ajax ,JavaScript or JQuery we need to uncomment or add following line before writing WebMethod
[System.Web.Script.Services.ScriptService]
Add jquery-1.7.2.min.js in application and reference it in Head section of page.
<head runat="server">
<script src="jquery-1.7.2.min.js" type="text/javascript"/>
</head>
Add and create Web Service by Right click on solution explorer > Add New Item > WebService.asmx.
I have created a WebMethod which takes and return string parameter in it.
C# CODE
01
[WebService(Namespace =
"http://tempuri.org/"
)]
02
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
03
[System.Web.Script.Services.ScriptService]
04
public
class
WebService : System.Web.Services.WebService
05
{
06
[WebMethod]
07
public
string
Welcome(
string
name)
08
{
09
return
"Welcome "
+ name;
10
}
11
}
VB.NET CODE
1
Public
Function
Welcome(name
As
String
)
As
String
2
Return
"Welcome "
& name
3
End
Function
4
End
Class
Place one text input html control, one input button type on page, you can place asp.net button control either.
Enter Name: <input type="text" id="txtName" />
<asp:Button ID="btn" runat="server" Text="Invoke WebService"
OnClientClick="CallWebService(); return false;"/>
<asp:Label ID="lblMsg" runat="server" Text=""/>
<input type="button" id="btnCall" value="Call Service"
onclick="CallWebService(); return false;"/>
Add following Script in Head section of page to call PageMethod using JQuery.
01
<script type=
"text/javascript"
>
02
function CallWebService()
03
{
04
$.ajax({
05
type:
"POST"
,
06
url:
"WebService.asmx/Welcome"
,
07
data:
"{'name':'"
+ $(
"#txtName"
).val() +
"'}"
,
08
contentType:
"application/json; charset=utf-8"
,
09
dataType:
"json"
,
10
success: ShowData,
11
error: error
12
});
13
}
14
function ShowData(data, status)
15
{
16
$(
"#lblMsg"
).html(data.d);
17
}
18
function error(request, status)
19
{
20
alert(request.statusText);
21
}
22
</script>
To call WebService using JavaScript, we can write script as follows
Place ScriptManager on the page and add ServiceReference and path in it.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
Write this Script in Head of page.
01
<script language=
"javascript"
type=
"text/javascript"
>
02
function CallService()
03
{
04
WebService.Welcome(form1.txtName.value, showData, err);
05
}
06
function showData(arg) {
07
alert(arg);
08
}
09
function err(arg) {
10
alert(
"error"
);
11
}
12
</script>
No comments:
Post a Comment