ASP.NET calling Webmethod using jQuery

To call Webmethod asynchronously in ASP.net through jQuery

Javascript Code


<script type=”text/javascript”>
function Authenticate() {
	$.ajax({
		type: "POST",
		url: "/login.aspx/Authenticate",
		data: "{ provide json data }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(msg) {
			if (msg.d == '1') {
				alert(‘authenticated’);
			} else {
				alert("not authenticated");
			}
		},
		error: function() {
			alert("Error :(");
		}
	});
};
</script>


Code in ASPX.cs file


[WebMethod]
public static int Authenticate(string UserID, string Password)
{
	// authenticate and return value
}


ASPX code


<a href=”javascript:Authenticate();” data-role=”button” data-theme=”b” data-inline=”true” runat=”server”>Login</a>

Leave a Reply

Your email address will not be published.