title: ASP.Net web service won't return JSON - Always XML
body: <p>I'm trying to create a simple AJAX &amp; web service test (using C# .Net 2.0) to return data in JSON format, and I have (I believe) everything I need but I keep running into the same problem over and over again.  The response from the web service is always XML.  (It always has <code>&lt;?xml version="1.0" encoding="utf-8"?&gt;</code> as the 1st line).</p>
  
  <p>I've tried the various options that come within the ScriptMethod tag, but nothing makes any difference.</p>
  
  <p>The call works okay, but I get a "parsererror" with the response, which I would expect as it's not valid JSON.  I can call the web service method within the browser and get the return value I expect, just in XML format.  Fiddler also shows that the request and response are as I've found.</p>
  
  <p>There are already several questions on here about the exact same issue, but none of them have given me a working answer.</p>
  
  <p>Please ask questions if you need more information.</p>
  
  <p><strong>C#</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.Services;
  using System.Web.Script.Services;
  using System.Web.Script.Serialization;
  
  namespace jQueryWebServiceTest
  {
      /// &lt;summary&gt;
      /// This is a simple web service to test calling from javascript using AJAX, and getting a response.
      /// &lt;/summary&gt;
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]
      [ScriptService]
      public class Service1 : System.Web.Services.WebService
      {
          [WebMethod]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public string SayHello()
          {
              //  I've tried both this...
              var response = new { value = "Hello" };
              JavaScriptSerializer json = new JavaScriptSerializer();
              return json.Serialize(response);
  
              //  and this...
              return "Hello";
          }
      }
  }
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>$(function () {
  
      var data = JSON.stringify({ Name: "John" });  // This is not used - part of other testing.
      data = "{}";
  
      $.ajax({
          type: "POST",
          data: data,
          url: "TestService.asmx/SayHello",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (data) {
              console.log(data);
          },
          error: function (error, status) {
              console.log("status = " + status);
              console.log(error);
          }
      });
  
  });
  </code></pre>