title: C# WebService return json
body: <p>I need to return a message from this web service bellow, but it has to be in json format. Is it possible to achieve this now?</p>
  
  <p>C# code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Web.Script.Services;
  
  namespace WebApplication1
  {
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]
      [System.Web.Script.Services.ScriptService]
      public class WebService1 : System.Web.Services.WebService
      {
  
          [WebMethod]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public response HelloWorld()
          {
              response obj = new response
              {
                  message = "No fromFormat recieved.",
                  success = false
              };
  
              return obj;
          }
      }
  
      public class response
      {
          public string message = "";
          public Boolean success = false;
      }
  }
  </code></pre>
  
  <p>jQuery code:</p>
  
  <pre><code>//contentType: "application/json" if I add this line, response is undefined.
  $('document').ready(function () {
      $.ajax({
          url: 'http://exampleUrl.com/WebService1.asmx/HelloWorld',
          type: 'POST',
          success: function (data) {
              console.log(data.responseText);
          },
          error: function (data) {
              console.log(data.responseText);
          },
          async: false,
          dataType: "json",
      });
  });
  </code></pre>
  
  <p>Response:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"&gt;
    &lt;message&gt;No fromFormat recieved.&lt;/message&gt;
    &lt;success&gt;false&lt;/success&gt;
  &lt;/response&gt; 
  </code></pre>