I have a standalone .ASPX page (no code behind) which holds a simple WebMethod that I'm calling via JQuery Ajax. See below:
(Please be aware this is proof-of-concept code, not production code!)
<!DOCTYPE html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head runat="server">
<title>Untitled</title>
<script runat="server" type="text/c#">
[System.Web.Services.WebMethod]
public static int GetData(int Id)
{
string connectionString = "Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT AVG(Number) FROM Data WHERE ID = @Id;";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@Id", Id);
connection.Open();
return (int)command.ExecuteScalar();
}
}
}
</script>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function getAvg() {
$.ajax({
type: 'POST',
url: "WebMethodAjaxExample.aspx/GetData",
data: JSON.stringify({Id: $('#tbId').val()}),
contentType: 'application/json',
success: function (data, textStatus) {
$('#theParagraph').text(data.d);
}
})
}
</script>
</head>
<body>
<div>
<input type="text" id="tbId" />
<button onclick="getAvg()">Go</button>
</div>
<p id="theParagraph"></p>
</body>
</html>
I've sanity checked the the SQL query and know that it returns a FLOAT (seven-and-a-bit). However, as you can see my WebMethod returns an int. Hence my page was always rounding the number.
I decided to change the WebMethod return type and the ExecuteScalar casting to Double. But still the page was returning "7".
After a bit of tinkering the most interesting fact I learned was that when I decided to change the WebMethod name in the C# code to GetDatum, and made the relevant change to the JQuery Ajax function name too, running the page this time I get a return status 500 from the Ajax call with "Unknown Web Method" as the error message.
It feels like the page is not being dynamically recompiled as I would expect it to be, instead it is still using a cached version even though the request header states "no-cache".
If it's of any help, the page is hosted in Sharepoint 2010.
Can anyone understand what's going on?
UPDATE: Recycling the application pool makes the most up to date WebMethod code work, but further updates are not reflected until the application pool is reset again.