Hi,
Today, I got the requirement where I need to call a method which is in Aspx from JavaScript and do some operation. Somehow, I felt it is very simple requirement and someone must have done this before. I did some search and there are many sources for it. This post is the combination of many posts...
Created a Asp.net Web application.
Added a code within the method as shown below. Make sure you give [WebMethod].
[WebMethod]
public static string WebServiceCall(string Value)
{
return "My name is: " + Value;
}
Used below the Jquery code to call the method which was written in .aspx.cs page.
Included.
function CallWebMethod() {
$.ajax({
type: "POST",
url: "/Test.aspx/WebServiceCall",
data: '{Value: "' + $("#<%=TextBox1.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
Added a HTML button in .aspx page and called the Jquery function.
<body>
<form id="form1" runat="server">
<asp:Label ID="lblFirstName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="btnClick" type="button" value="Click Me!" onclick="CallWebMethod();" />
</form>
</body>
</html>
When I execute the code and click on the button I got the exception as "Authentication Failed."
Fix for this : Goto -- App_Start -- RouteConfig and comment the below line as per the screen shot.
Now the code should work properly.
Hope this helps.
--
Happy Coding
Gopinath.
Today, I got the requirement where I need to call a method which is in Aspx from JavaScript and do some operation. Somehow, I felt it is very simple requirement and someone must have done this before. I did some search and there are many sources for it. This post is the combination of many posts...
Created a Asp.net Web application.
Added a code within the method as shown below. Make sure you give [WebMethod].
[WebMethod]
public static string WebServiceCall(string Value)
{
return "My name is: " + Value;
}
Used below the Jquery code to call the method which was written in .aspx.cs page.
Included.
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">function CallWebMethod() {
$.ajax({
type: "POST",
url: "/Test.aspx/WebServiceCall",
data: '{Value: "' + $("#<%=TextBox1.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
Added a HTML button in .aspx page and called the Jquery function.
<body>
<form id="form1" runat="server">
<asp:Label ID="lblFirstName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="btnClick" type="button" value="Click Me!" onclick="CallWebMethod();" />
</form>
</body>
</html>
When I execute the code and click on the button I got the exception as "Authentication Failed."
Fix for this : Goto -- App_Start -- RouteConfig and comment the below line as per the screen shot.
Now the code should work properly.
Hope this helps.
--
Happy Coding
Gopinath.
No comments:
Post a Comment