title: Using jQuery to POST Form Data to an ASP.NET ASMX Web Service
body: <p>I try to develop a simple login form where submit button post form data to my method in asmx file. <br>
  The problem is that I get error as soon I try to post from inside a forms tags. <br>
  <strong>Posting outside form tags works fine.</strong><br>
  I have provided a simplified example of my problem: </p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
  $(document).ready(function () {
  
      $("#submitOutsideForm").click(function () {
          AjaxPost();
      });
  
      $("#submitInsideForm").click(function () {
          AjaxPost();
      });
  });
  
  function AjaxPost() {
      try {
          $.ajax({
              type: "POST",
              url: "dummyWS.asmx/HelloToYou",
              data: "{'name': '" + $('#name').val() + "'}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                  AjaxSucceeded(msg);
              },
              error: AjaxFailed
          });
      }
      catch (e) {
          alert("Exception in ajaxCall2Func: " + e.name + ".\n Error messege: " + e.message);
      }
  }
  
  function AjaxSucceeded(result) {
      alert(result.d);
  }
  
  function AjaxFailed(result) {
      alert(result.status + ' ' + result.statusText);
  }
  &lt;/script&gt;  
  &lt;/head&gt;
  &lt;body&gt;
  
  Enter your name: 
  &lt;input id="name" /&gt;
  
  &lt;form id="form1" runat="server" style="border:1px solid green;"&gt;
  &lt;div&gt;
      &lt;label for="email"&gt;some other form controls&lt;/label&gt;
  &lt;/div&gt;
  &lt;input id="submitInsideForm" type="submit" value="Submit Inside Form" /&gt;
  &lt;/form&gt;
  &lt;input id="submitOutsideForm" type="submit" value="Submit Outside Form" /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>On server side I have this simple dummy WebService </p>
  
  <pre><code>&lt;%@ WebService Language="C#" CodeBehind="~/App_Code/dummyWS.cs" Class="dummyWS" %&gt;
  </code></pre>
  
  <p>…and code behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.Web.Script.Services.ScriptService]
  public class dummyWS : System.Web.Services.WebService {
  
    [WebMethod()]
    public string HelloToYou(string name)
    {
        return "Hello " + name;
    }
  }
  </code></pre>