title: Pass Variable from $.ajax() to ASMX Web Service
body: <p>Can someone show me how to pass a variable from a $.ajax() call to an ASMX Web Service?</p>
  
  <p>Web Service:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Data.SqlClient;
  
  public class Psyience
  {
      public int ID;
      public string Choice;
      public string First;
      public string Last;
      public string Email;
  }
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  
  [System.Web.Script.Services.ScriptService]
  [System.Web.Script.Services.GenerateScriptType(typeof(Psyience))]
  public class CRUD : System.Web.Services.WebService
  {
  
      [WebMethod]
      public Psyience[] Anchor()
      {
          List&lt;Psyience&gt; items = new List&lt;Psyience&gt;();
          string constr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myConString"].ConnectionString;
          SqlConnection con = new SqlConnection(constr);
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandText = "select Choice from tbl_Psyience WHERE Choice = 'Anchor'";
          try
          {
              con.Open();
  
              SqlDataReader dr = cmd.ExecuteReader();
              while (dr.Read())
              {
                  Psyience item = new Psyience();
                  item.Choice = dr["Choice"].ToString();
                  items.Add(item);
              }
              con.Close();
              return items.ToArray();
          }
          catch
          {
              return null;
          }
      }
  
  }
  </code></pre>
  
  <p>And here's the jQuery:</p>
  
  <pre><code>$(document).ready(function () {
      $.ajax({ type: "POST",
          url: "Services/CRUD.asmx/Anchor",
          data: "{ }",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (response) {
              var result = response.d;
              var myRecord = "";
              $.each(result, function (index, res) {
                  myRecord = res.Choice;
              $('#myDiv').append(myRecord);
              });
  
          },
          error: function (msg) {
              $('#myErr').html("Error while calling web service,,")
          }
      });
  });
  </code></pre>
  
  <p>I'd like to pass a string value from the jQuery to my command text in my web service. </p>
  
  <p>"select Choice from tbl_Psyience WHERE Choice = 'passVarToHere'"</p>
  
  <p>Not sure how to accomplish this.</p>
  
  <p>Thanks in advance.</p>