title: ASP.NET jQuery Ajax Calling Code-Behind Method
body: <p>I am <em>very</em> new to web development, but have a lot of experience in development in general. I have an ASP page that has a few input fields and a submit button. This submit button purely calls $.ajax, which I intended to have call a method in the code-behind file. However, I've noticed two interesting things. First, the ajax call succeeds regardless of what data is provided to it. Secondly, the responseText field is the entire page's html source.</p>
  
  <p>I've read <a href="https://stackoverflow.com/questions/583116/call-asp-net-pagemethod-webmethod-with-jquery-returns-whole-page">this</a> and other articles that point to the webconfig, but these solutions do not seem to resolve my issue.</p>
  
  <p>Here is the asp page:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="TesScript.js"&gt;&lt;/script&gt;
      &lt;link rel="Stylesheet" type="text/css" href="TestStyle.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;
          &lt;ul class="tempList"&gt;
              &lt;li&gt;Name:
                  &lt;input id="nameText" type="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;Attending:
                  &lt;input id="yesButton" type="radio" name="attending" /&gt;
                  Yes
                  &lt;input id="noButton" type="radio" name="attending" /&gt;
                  No &lt;/li&gt;
              &lt;li&gt;Return Address:
                  &lt;input id="returnAddressText" type="text" /&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;input id="submitButton" type="button" onclick="submit()" value="Submit" /&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;ul id="errorContainer" class="errorSection" runat="server" /&gt;
      &lt;ul id="messageContainer" class="messageSection" runat="server" /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The code behind:</p>
  
  <pre><code>using System;
  using System.Web.Services;
  using System.Web.UI;
  
  namespace TestAspStuff
  {
      public partial class _Default : Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
          }
  
  
          [WebMethod]
          public static string OnSubmit(string name, bool isGoing, string returnAddress)
          {
              return "it worked";
          }
      }
  }
  </code></pre>
  
  <p>And the JavaScript:</p>
  
  <pre><code>function submit() {
  
      var name = "my name";
      var isAttending = true;
      var returnAddress = "myEmail@gmail.com";
  
      SendMail(name, isAttending, returnAddress);
  }
  
  function SendMail(person, isAttending, returnEmail) {
  
      var dataValue = { "name": person, "isGoing": isAttending, "returnAddress": returnEmail };
  
      $.ajax({
          type: "POST",
          url: "Default.aspx/OnSubmit",
          data: dataValue,
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
          },
          complete: function (jqXHR, status) {
              alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
          }
      });
  
  }
  </code></pre>
  
  <p>Now, I noticed I can change the url property to anything I want and the error method is never called, and the status is success, with the responseText being the entire html page. My webconfig has all of the appropriate sections (including the htmlModule section). I am working in .Net 3.5. I appreciate any help and, again, I'm really new to this so what's obvious to others is most likely not obvious to me. And if there's a better way to do this (calling asp.net code-behind methods from JavaScript, that is) please feel free to post that. Thanks!!!</p>