title: How to loop through POST variables sent to ASP.NET WebMethod from JQuery $.ajax method?
body: <p>I have a checklist created with HTML buttons that toggle between 'selected' and 'unselected' classes on click. Each option has an ID related with it and also an optional comment input. </p>
  
  <p>I am trying to use the JQuery $.ajax() method with POST to send the IDs of the selected buttons and the related comment. The idea I have is to create a key/value collection of the IDs selected and their respective Comments when 'Save' is clicked like so:</p>
  
  <pre><code>selections = "{id1: 'comment1', id2: 'comment2', ...}"
  </code></pre>
  
  <p>Then pass it to the data portion of the $.ajax() method like so:</p>
  
  <pre><code>$.ajax({
       type: "POST",
       url: "MyPage.aspx/MyMethod"
       data: selections,
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function(msg) {
         onSuccess(msg);
       }
  });
  </code></pre>
  
  <p>And finally access the selections in <code>MyMethod</code> by looping through <code>HttpContext.Current.Request.Form</code>.</p>
  
  <p>But when I try this, <code>HttpContext.Current.Request.Form</code> is always empty. Is my approach wrong? If so, why? Can anyone suggest another approach? </p>
  
  <p>Any help would be greatly appreciated. Thanks in advance!</p>
  
  <p>Also, I know of 4 ways to call a server method from the JQuery AJAX API:</p>
  
  <ol>
  <li>Page Method (static method in a .aspx page with the <code>[WebMethod]</code> modifier)</li>
  <li>Switch statement between methods in <code>Page_Load</code> of a .aspx page</li>
  <li>Switch statement between methods in <code>ProcessRequest</code> of a .ashx page</li>
  <li>Web Service</li>
  </ol>
  
  <p>Which way is most efficient? I assume 2 would involve an unnecessary instance of a Page, so that shouldn't be the one. </p>