title: ASP.NET 4.0 - Using Jquery, how do I call a codebehind function and return data to the client?
body: <p>When I click on the following div:</p>
  
  <pre><code> &lt;div id="Result"&gt;Click here for the time.&lt;/div&gt;
  </code></pre>
  
  <p>I need the following codebehind function to run:</p>
  
  <pre><code> &lt;WebMethod()&gt; _
   Public Shared Function GetDate() As String
       Return DateTime.Now.ToString()
   End Function
  </code></pre>
  
  <p>I need this to populate the inside of the div with the string returned by the GetDate() function. I think this should use code similar to this:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          // Add the page method call as an onclick handler for the div.
          $("#Result").click(function () {
              $.ajax({
                  type: "POST",
                  url: "test.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);
                  }
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>I've pulled this example from this site: <a href="http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/" rel="nofollow">http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/</a></p>
  
  <p>However, I simply cannot get it to work. Nothing happens. This is just a regular asp.net web project. I haven't done any sort of Ajax-enabling business other than including script tags in my markup to reference jquery. </p>
  
  <p>Here's what the firebug console tells me when I click on the div:</p>
  
  <pre><code> POST http://admin/Default.aspx   GetDate   404 Not Found   -18ms
  </code></pre>
  
  <p>Edit: Note: test.aspx/GetDate must match your aspx page name and function name!</p>