title: asp.net asmx web service returning xml instead of json
body: <p>Why does this simple web service refuse to return JSON to the client?</p>
  
  <p>Here is my client code:</p>
  
  <pre class="lang-js prettyprint-override"><code>        var params = { };
          $.ajax({
              url: "/Services/SessionServices.asmx/HelloWorld",
              type: "POST",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              timeout: 10000,
              data: JSON.stringify(params),
              success: function (response) {
                  console.log(response);
              }
          });
  </code></pre>
  
  <p>And the service:</p>
  
  <pre class="lang-c prettyprint-override"><code>namespace myproject.frontend.Services
  {
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]
      [ScriptService]
      public class SessionServices : System.Web.Services.WebService
      {
          [WebMethod]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public string HelloWorld()
          {
              return "Hello World";
          }
      }
  }
  </code></pre>
  
  <p>web.config:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;configuration&gt;
      &lt;system.web&gt;
          &lt;compilation debug="true" targetFramework="4.0" /&gt;
      &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>And the response:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;string xmlns="http://tempuri.org/"&gt;Hello World&lt;/string&gt;
  </code></pre>
  
  <p>No matter what I do, the response always comes back as XML. How do I get the web service to return Json?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the Fiddler HTTP trace:</p>
  
  <pre class="lang-none prettyprint-override"><code>REQUEST
  -------
  POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
  Host: myproject.local
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
  Accept: application/json, text/javascript, */*; q=0.01
  Accept-Language: en-gb,en;q=0.5
  Accept-Encoding: gzip, deflate
  Connection: keep-alive
  Content-Type: application/json; charset=utf-8
  X-Requested-With: XMLHttpRequest
  Referer: http://myproject.local/Pages/Test.aspx
  Content-Length: 2
  Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
  Pragma: no-cache
  Cache-Control: no-cache
  
  {}
  
  RESPONSE
  -------
  HTTP/1.1 200 OK
  Cache-Control: private, max-age=0
  Content-Type: text/xml; charset=utf-8
  Server: Microsoft-IIS/7.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Date: Tue, 19 Jun 2012 16:33:40 GMT
  Content-Length: 96
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;string xmlns="http://tempuri.org/"&gt;Hello World&lt;/string&gt;
  </code></pre>
  
  <p>I have lost count of how many articles I have read now trying to fix this. The instructions are either incomplete or do not solve my issue for some reason.
  Some of the more relevant ones include (all without success):</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4842020/asp-net-web-service-erroneously-returns-xml-instead-of-json">ASP.NET web service erroneously returns XML instead of JSON</a></li>
  <li><a href="https://stackoverflow.com/questions/2749954/asmx-web-service-returning-xml-instead-of-json-in-net-4-0">asmx web service returning xml instead of json in .net 4.0</a></li>
  <li><a href="http://williamsportwebdeveloper.com/cgi/wp/?p=494" rel="noreferrer">http://williamsportwebdeveloper.com/cgi/wp/?p=494</a></li>
  <li><a href="http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/" rel="noreferrer">http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/</a></li>
  <li><a href="http://forums.asp.net/t/1054378.aspx" rel="noreferrer">http://forums.asp.net/t/1054378.aspx</a></li>
  <li><a href="http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/" rel="noreferrer">http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/</a></li>
  </ul>
  
  <p>Plus several other general articles.</p>