skip to Main Content

I have a strange problem where I have two webmethods in a webform class these are being called by JQuery AJAX but I am getting a Unknown web method error. I have tried making the webmethods public and static but I cannot get this to work. I am testing this locally. Can someone help please, thanks!

aspx

public partial class PBXservice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public class PBXservices : System.Web.Services.WebService
   {
        [WebMethod]
        public static string service(string data)
        {
            //connect client to url where SignalR server is listening
            var hubConnection = new HubConnection("http://localhost:8081/");
            var serverHub = hubConnection.CreateHubProxy("WebHub");

            //start Hub and wait to finish connecting
            hubConnection.Start().Wait();

            //call Hub with Invoke, pass name and argument
            serverHub.Invoke("Connect", "PBXWebApp");
            // string line = null;
            while ((data = System.Console.ReadLine()) != null)
            {
                // send message to the server
                serverHub.Invoke("SendPBXData", data, "PBXWebApp").Wait();
            }
            return data;
        }

        [System.Web.Services.WebMethod]
        public static string GetData()
        {
            return DateTime.Now.ToString();
        }
    }

}

AJAX

   var pbxdata = "Hi there!";
        jQuery.ajax({
            url: 'PBXservice.aspx/service',
            type: "POST",
            dataType: "json",
            data: "{'pbxdata': '" + pbxdata + "'}",
            contentType: "application/json; charset=utf-8", 
            success: function (data) {
                alert(JSON.stringify(data));
            }
        });




 $.ajax({  
          type: "POST",  
          url: "PBXservice.aspx/GetData",  
          data: '',  
          contentType: "application/json; charset=utf-8",  
          dataType: "json",  
          success: function (response) {  
              alert(response.d);  
          },  
 });  

web config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <pages controlRenderingCompatibilityVersion="4.0" />
  </system.web>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
<system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer></configuration>

2

Answers


  1. Chosen as BEST ANSWER

    I had to change the Codebehind attribute to CodeFile and that was it!

    CodeBehind="PBXservice.aspx.cs"

    to

    CodeFile="PBXservice.aspx.cs"

    Parser Error Message: Could not load type 'webmarketing'

    Thanks to all for posting!


  2. Good Morning. Do you really need this “PBXservices” class? You could put the “service” and “GetData” methods directly in “PBXservice”. Then change the configuration in “App_Code RouteConfig.cs”:
    OLD: “settings.AutoRedirectMode = RedirectMode.Permanent”
    NEW: “settings.AutoRedirectMode = RedirectMode.Off”.

    ASPX.CS

    
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Method intentionally left empty.
            }
    
            [WebMethod]
            public static string GetData()
            {
                return DateTime.Now.ToString();
            }
        }
    
    

    ASPX (HTML)

    
         
            $(document).ready(function () {
                $('#bntAjax').click(CallAjax);
            });
    
            function CallAjax() {
                $.ajax({
                    type: "POST",
                    url: "PBXservices/GetData",
                    data: '',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        console.log(response);
                    },
                });
            }
        
    
    

    App_CodeRouteConfig.cs

        public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                var settings = new FriendlyUrlSettings();
                settings.AutoRedirectMode = RedirectMode.Off;
                routes.EnableFriendlyUrls(settings);
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search