0

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&amp; completedSynchronously) +69

Is what i'm trying to do even possible?

Community
  • 1
  • 1
Scott
  • 1,280
  • 4
  • 20
  • 35

2 Answers2

0

You must annotate your method so the compiler knows that it is a PageMethod and so accessible from jQuery.

[System.Web.Services.WebMethod]
public static string SayHello(string name) { ... }

Converting to asmx web service:

[System.Web.Script.Services.ScriptService]
public class Ajax2 : System.Web.Services.WebService
{
    [WebMethod]
    public string SayHello(string name)
    {
        return "Hello " + name; 
    }
}
Dean Parker
  • 38
  • 1
  • 5
  • Hi @parker130, i tried adding the annotation but its still Unknown web method sayhello. – Scott Aug 14 '15 at 10:42
  • Check the URL. Method names are likely to be case-sensitive, and so the method should called using "Example2.aspx/SayHello". – Dean Parker Aug 14 '15 at 10:49
  • Yeah i tried both sayhello and SayHello but still getting the error. Ok so it works if i move the method to code behind but not to an external class. Is there a problem with how i'm communicating with that class? – Scott Aug 14 '15 at 10:55
  • PageMethods are more suitable when you are exposing functionality within the same page. In this case, Example2.aspx doesn't have any page methods, but a class called Ajax2 does. Once you start separating your ajax methods from your page, you're into dedicated web service territory, either ASMX or WCF. I've edited my answer to demonstrate how you could convert your class to be an ASMX web service. You call the method in the same way from jQuery I.e. "Ajax2.asmx/SayHello" – Dean Parker Aug 14 '15 at 11:12
  • Thanks parker i've got it working using a .asmx file – Scott Aug 14 '15 at 11:43
0

When You want to call method of backed using Ajax call you have to make [WebMethod]

  [WebMethod]
  public static string SayHello(string name)
        {
            return "Hello " + name;
        }