1

I am trying to use jQuery to directly call ASP.NET AJAX page methods. I'm using encosia.com as a reference. My inline javascript is

       <script type="text/javascript">
           $(document).ready(function() {
               // Add the page method call as an onclick handler for the div.
               $("#Result").click(function() {                   
                   $.ajax({
                       type: "POST",
                       url: "Default.aspx/GetDate",
                       data: "{}",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function(msg) {
                           // Replace the div's content with the page method's return.
                           $("#Result").text(msg.d);
                       }
                   });
               });
           });

            </script>
       <div id="Result">Click here for the time.</div> 

with my webmethod being

<WebMethod()> _
Public Shared Function GetDate() As String
    Return DateTime.Now.ToString()
End Function

I would use FF and check POST sent, but since where I'm at currently only has IE 7 this is a little hard to do. Other relevent information, ASP.net 2.0. Does anyone know what I'm doing wrong?


Update

web.config - pre-existing still not working

<httpModules>
  <remove name="FormsAuthentication" />
  <remove name="PassportAuthentication" />
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
atrueresistance
  • 1,358
  • 5
  • 26
  • 48
  • 1
    er...download firefox anyway? or get Tamper Data or Fiddler or something to see if your call is being made. also you could do an alert or console log in your success and also make an error function and alert there to see what the problem is – Evan Jun 06 '12 at 15:30
  • So what's not working? Have you tried making your `GetDate` webmethod `static`? – Jason Towne Jun 06 '12 at 15:38
  • As a side note, you may want to consider using generic handlers (.ashx) files for your AJAX calls instead of a regular .NET page. When using regular .NET pages you're still going to go through the entire page life cycle when calling the web method. Generic handlers don't go through the page life cycle saving you some overhead. – Jason Towne Jun 06 '12 at 15:45
  • @JasonTowne when changing it to static function it won't compile. I believe static is only if you code the page in C# – atrueresistance Jun 06 '12 at 16:05
  • @atrueresistance Ah, OK. I code primarily in C# and it's been years since I worked with VB. Thanks for the heads up. – Jason Towne Jun 06 '12 at 16:17
  • @JasonTowne: The approach he's using here does circumvent the normal ASPX page life cycle. It's basically shorthand for declaring an ASMX ScriptService inline inside the ASPX code behind. It's slightly less efficient than a raw handler, but only marginally so (and has some built-in perks like automatic JSON [de]serialization for parameters and the response). – Dave Ward Jun 07 '12 at 01:46
  • @DaveWard Good to know. You learn something new every day. Thanks for the heads up. – Jason Towne Jun 07 '12 at 14:39

3 Answers3

5

Since you're using ASP.NET 2.0, you'll need to install the ASP.NET AJAX Extensions, like Joe Enos mentioned. I have a bit more info on the necessary configuration work here: http://encosia.com/asmx-scriptservice-mistakes-installation-and-configuration/

Additionally, the .d wrapper around the response is an addition that didn't come until ASP.NET 3.5. So, even if you've got everything else working right, your msg.d will be undefined in ASP.NET 2.0. Omit the .d and just make that:

success: function(msg) {
  // Replace the div's content with the page method's return.
  $("#Result").text(msg);
}
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
1

In .NET 2.0, you need to make sure you install the ASP.NET AJAX extensions. In .NET 3.5 and 4.0, the encosia solution works without any modifications.

I don't remember exactly what you need to install, but it might be this.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
0

Have you checked your web.config to allow page methods?

<system.web>
  <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </httpModules>
</system.web>

There is a similar issue here at this post here

Community
  • 1
  • 1
leon.io
  • 2,779
  • 1
  • 18
  • 26