Skip to content

Instantly share code, notes, and snippets.

@smetronic
Created December 3, 2016 06:21
Show Gist options
  • Save smetronic/6a47ebd3275ff66ec654dd0ba9277d9f to your computer and use it in GitHub Desktop.
Save smetronic/6a47ebd3275ff66ec654dd0ba9277d9f to your computer and use it in GitHub Desktop.
Calling ASP.Net Page Method using jQuery AJAX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Ajax.Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
</form>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$.ajax({
type: "POST",
url: "Default.aspx/GetResult",
data: '{variable: 123456 }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function(response) {
alert(response.d);
}
});
</script>
</body>
</html>
using System;
using System.Web.Services;
namespace Ajax
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetResult(int variable)
{
return "Data " + variable;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment