title: Call ASP.NET WebMethod using AJAX JSON
body: <p>I'm prototyping a new web app in ASP.NET with VB.NET codebehind that uses JQuery's AJAX to call a WebMethod, but I keep getting parsererrors. Here's my markup:</p>
  
  <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="testpage.aspx.vb" Inherits="WorkOrder_testpage" %&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head id="Head1" runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;link href="../jquery-ui-1.9.2.custom.min.css" rel="stylesheet" /&gt;
      &lt;script src="../jquery-1.8.3.js"&gt;&lt;/script&gt;
      &lt;script src="../jquery-ui-1.9.2.custom.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $(document).ready(function () {
              $('#btnTest').click(function () {
                  $.ajax({
                      type: 'POST',
                      contentType: 'application/json; charset=utf-8',
                      url: 'testpage.aspx/testFunction',
                      //data: {a:'hello'},
                      dataType: 'json',
                      success: function (data) {
                          $('#lblDebug').text(data);
                      },
                      error: function (jqXHR, textStatus, errorThrown) {
                          alert(textStatus + ": " + errorThrown);
                      }
                  });
              });
              $('#btnTest').focus();
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /&gt;
          &lt;span id="lblDebug"&gt;&lt;/span&gt;&lt;input type="button" id="btnTest" value="TEST" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my codebehind:</p>
  
  <pre><code>Imports System.Web.Services
  Imports System.Web.Script.Serialization
  Partial Class WorkOrder_testpage
      Inherits System.Web.UI.Page
      &lt;WebMethod()&gt; _
      Public Shared Function testFunction() As String
          Dim jsonSer As New JavaScriptSerializer
          testFunction = jsonSer.Serialize("hello")
      End Function
  End Class
  </code></pre>
  
  <p><strong>Things I have tried already:</strong></p>
  
  <ul>
  <li>omitting the dataType and/or contentType. This results in HTML being passed back upon success</li>
  <li>using "data.d" instead of "data" when outputting results on success. Only works when omitting dataType and/or contentType, no data shown.</li>
  <li>Adding an httpModules element to the web.config. No change, however there may be more I'm missing there.</li>
  <li>formatting my return data in just about every way imaginable. Always ends in a parsererrror. It always says, "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data"</li>
  </ul>
  
  <p><strong>Where I THINK I'm going wrong:</strong></p>
  
  <p>I think either I'm still not formatting my JSON right, in which case please illuminate me, or there is some configuration I am unaware of that is preventing my JSON from getting through.</p>
  
  <p>I know this is similar to about 80 other questions, but none I have found so far have seemed to be of any help.</p>
  
  <p>My sources of help:<br>
  <a href="https://stackoverflow.com/questions/11269622/asp-net-jquery-ajax-json-simple-example-of-exchanging-data">asp.net jquery ajax json: Simple example of exchanging data</a>
  <a href="https://stackoverflow.com/questions/4954628/pagemethods-jquery-and-json">PageMethods, jQuery and JSON</a>
  <a href="http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/" rel="nofollow noreferrer">http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/</a>
  <a href="http://www.aspdotnet-suresh.com/2013/12/jquery-ajax-json-example-in-aspnet.html" rel="nofollow noreferrer">http://www.aspdotnet-suresh.com/2013/12/jquery-ajax-json-example-in-aspnet.html</a><br>
  And many many more.</p>
  
  <p><strong>Update 6/10/14</strong></p>
  
  <p>I tried Mun's answer below with similar/same results. If anybody has done this with the same structure, I would like to know how much, if any, you had to add or configure in your web.config file.</p>