title: Sending Key Value Pairs as JSON to ASMX service with jquery
body: <p>I have a been working on a contact form I am new to ASP.net, I know an intermediate amount of C# I am working on a contact form. I want to send the Values as a json array and parse it with JSON.net, I tried every way I could think of to get it to work. Without success, I need to know how to properly send and receive JSON from an ASMX page. Is there an Example file or maybe a tutorial? Or can some one please tell me what I am doing wrong? This was the only way I could get it to read the post vars.</p>
  
  <p>But Its just 2 arrays not key value pairs.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;script src="Scripts/jquery-2.0.3.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
   &lt;body&gt;
      &lt;form action="#"&gt;
          &lt;input type="text" id="firstname" name="firstname" value=""/&gt;
          &lt;input type="text" id="lastname" name="lastname" value=""/&gt;
       &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;
   &lt;script&gt;
  
  $(document).ready(function () {
  
      var $serialize = $('form').serializeArray();
      var stringify = JSON.stringify($serialize);
  
      var keys = ['firstname', 'lastname'];
      var list = [$('#firstname').val(), $('#lastname').val()];
  
      var jsonText = JSON.stringify({ args: list, keys: keys });
  
      $.ajax({
  
          url: "validation.asmx/sendRequest",
          method: "POST",
          dataType: "json",
          data:jsonText,
          cache: false,
          processData: true,
          contentType: "application/json; charset=utf-8"
      }).done(function (data) {
  
          console.log(data);
  
      });
  
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Here is my asmx file, </p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using Newtonsoft.Json;
      using System.Web.Script.Services;
      using System.Data;
  
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  
      [System.Web.Script.Services.ScriptService]
       public class validation : System.Web.Services.WebService {
  
          public validation () {
  
         //Uncomment the following line if using designed components 
         //InitializeComponent(); 
      }
  
      [WebMethod]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public string sendRequest(List&lt;string&gt; args, List&lt;string&gt; keys)
      {
  
         var arg = args;
         var key = keys;
  
         return args[0];
  
       }
  
     }
  </code></pre>
  
  <p>here is my web config file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
       &lt;compilation debug="true" targetFramework="4.5"/&gt;
        &lt;httpRuntime targetFramework="4.5"/&gt;
         &lt;webServices&gt;
             &lt;protocols&gt;
               &lt;add name="HttpGet"/&gt;
               &lt;add name="HttpPost"/&gt;
             &lt;/protocols&gt;
         &lt;/webServices&gt;
      &lt;/system.web&gt;
    &lt;/configuration&gt;
  </code></pre>