Following on from this solution https://stackoverflow.com/a/4508430/1622376 .
I'm trying to modify it to use the sayhello method from an external class by:
Example2.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<%@ Register Assembly="MyClasses" Namespace="MyClasses" TagPrefix="test" %>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<test:Ajax2 runat="server" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$.ajax({
type: 'POST',
url: 'Example2.aspx/sayhello',
data: JSON.stringify({ name: 'Scott' }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
// Notice that msg.d is used to retrieve the result object
alert(msg.d);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
alert(thrownError);
}
});
});
});
</script>
</body>
</html>
then my class with the sayhello method is:
using System;
using System.Web;
using System.Text;
namespace MyClasses
{
public class Ajax2 : System.Web.UI.Page
{
public static string SayHello(string name)
{
return "Hello " + name;
}
}
}
i've added an the ajax error funtion to give some information on why the request is failing:
<title>Unknown web method sayhello.<br>Parameter name: methodName</title>
[ArgumentException: Unknown web method sayhello.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +488515
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +162
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
Is what i'm trying to do even possible?