title: .NET ASMX - Returning Pure JSON?
body: <p>I am going nuts here.  I've looked at the following entries and <strong>none</strong> of them are correcting the aberrant behavior I am seeing:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/288850/how-to-return-json-from-a-2-0-asmx-web-service">How to return JSON from a 2.0 asmx web service</a></li>
  <li><a href="https://stackoverflow.com/questions/1678101/how-to-return-json-from-asp-net-asmx">How to return JSON from ASP.NET .asmx?</a></li>
  <li><a href="https://stackoverflow.com/questions/211348/how-to-let-an-asmx-file-output-json">How to let an ASMX file output JSON</a></li>
  </ul>
  
  <p>I've also looked at, and confirmed my setup:  <a href="http://www.asp.net/AJAX/documentation/live/ConfiguringASPNETAJAX.aspx" rel="nofollow noreferrer">http://www.asp.net/AJAX/documentation/live/ConfiguringASPNETAJAX.aspx</a></p>
  
  <p>Here is my code (ASMX code behind):</p>
  
  <pre><code>namespace RivWorks.Web.Services
  {
      /// &lt;summary&gt;
      /// Summary description for Negotiate
      /// &lt;/summary&gt;
      [WebService(Namespace = "http://rivworks.com/webservices/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [ToolboxItem(false)]
      // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
      [ScriptService]
      public class Negotiate : System.Web.Services.WebService
      {
          [WebMethod]
          [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
          public RivWorks.Data.Objects.rivProduct GetSetup(string jsonInput)
          {
              // Deserialize the input and get all the data we need...
              // TODO:  This is a quick hack just to work with this for now...
              char[] tokens = { '(', '{', '}', ')', ',', '"' };
              string[] inputs = jsonInput.Split(tokens);
              string inputRef = "";
              string inputDate = "";
              string inputProductID = "";
              for (int i = 0; i &lt; inputs.Length; i++)
              {
                  if (inputs[i].Equals("ref", StringComparison.CurrentCultureIgnoreCase))
                      inputRef = inputs[i+2];
                  if (inputs[i].Equals("dt", StringComparison.CurrentCultureIgnoreCase))
                      inputDate = inputs[i+2];
                  if (inputs[i].Equals("productid", StringComparison.CurrentCultureIgnoreCase))
                      inputProductID = inputs[i+2];
              }
  
              Guid pid = new Guid(inputProductID);
              RivWorks.Data.Objects.rivProduct product = RivWorks.Data.rivProducts.GetProductById(pid);
  
              return product;
          }
      }
  </code></pre>
  
  <p>When I run this from my localhost instance I am getting this result set:</p>
  
  <pre><code>  &lt;ResultSet&gt;
      &lt;uiType&gt;modal&lt;/uiType&gt;
      &lt;width&gt;775&lt;/width&gt;
      &lt;height&gt;600&lt;/height&gt;
      &lt;swfSource&gt;
      http://localhost.rivworks.com/flash/negotiationPlayer.swf
      &lt;/swfSource&gt;
      &lt;buttonConfig&gt;
      http://cdn1.rivworks.com/Element/Misc/734972de-40ae-45f3-9610-5331ddd6e8f8/apple-logo-2.jpg
      &lt;/buttonConfig&gt;
    &lt;/ResultSet&gt;
  </code></pre>
  
  <p>What am I missing???</p>
  
  <hr>
  
  <p>NOTE:  I am using the 3.5 framework (or at least I thought I was as everything in my web.config is marked for 3.5.0.0)</p>
  
  <hr>
  
  <p>UPDATE:  I am browsing to the service and using the input box on that page.  You can try here:  <a href="http://dev.rivworks.com/services/Negotiate.asmx?op=GetSetup" rel="nofollow noreferrer">http://dev.rivworks.com/services/Negotiate.asmx?op=GetSetup</a>.  We are also trying to access it from a JS based web app running on another site (the main purpose of this particular service).  I do not have the code for that here.  (Sorry, the test form is only available from localhost.)</p>
  
  <hr>
  
  <p>UPDATE:  I added the following test page (JsonTest.htm) to try to see what was going back and forth.  All I get is a 500 error!  I even tried attaching to the process and break into my service.  THe 500 error is thrown before the ASP pipeline ever gets into my code.</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
      &lt;script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.js" type="text/javascript"&gt;&lt;/script&gt;
  
      &lt;script language="javascript" type="text/javascript"&gt;
          function sendReq() {
              alert("Before AJAX call");
              $.ajax(
              {
                  type: "POST"
                  , url: "http://kab.rivworks.com/Services/Negotiate.asmx/GetSetup"
                  , data: "{ \"ref\":\"http://www.rivworks.com/page.htm\", \"dt\":\"Mon Dec 14 2009 10:45:25 GMT-0700 (MST)\", \"productId\":\"5fea7947-251d-4779-85b7-36796edfe7a3\" }"
                  , contentType: "application/json; charset=utf-8"
                  , dataType: "json"
                  , success: GetMessagesBack
                  , error: Failure
              }
              );
              alert("After AJAX call");
          }
          function GetMessagesBack(data, textStatus) {
              alert(textStatus + "\n" + data);
          }
          function Failure(XMLHttpRequest, textStatus, errorThrown) {
              alert(textStatus + "\n" + errorThrown + "\n" + XMLHttpRequest);
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="test"&gt;Bleh&lt;/div&gt;
      &lt;a href="javascript:sendReq()"&gt;Test it&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Why is this so painfully hard?!?!  :)</p>
  
  <hr>
  
  <p>UPDATE:  Working through a WCF service.  Here is my setup:
  <em>Interface:</em></p>
  
  <pre><code>namespace RivWorks.Web.Services
  {
      [ServiceContract(Name = "Negotiater", Namespace = "http://www.rivworks.com/services")]
      public interface INegotiaterJSON
      {
          //[WebMethod]
          [OperationContract]
          [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
          [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
          ResultSet GetSetup(string jsonInput);
      }
  }
  </code></pre>
  
  <p><em>Class:</em></p>
  
  <pre><code>namespace RivWorks.Web.Services
  {
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
      public class Negotiater : INegotiaterJSON
      {
          public ResultSet GetSetup(string jsonInput)
          {
              //code removed for brevity - see ASMX code above if you are really interested.
              return resultSet;
          }
      }
  
  
      [DataContract()]
      public class ResultSet
      {
          [DataMember]
          public string uiType = "modal";
          [DataMember]
          public int width = 775;
          [DataMember]
          public int height = 600;
          [DataMember]
          public string swfSource = "";
          [DataMember]
          public string buttonConfig = "";
      }
  }
  </code></pre>
  
  <p><em>web.config</em></p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name ="soapBinding"&gt;
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="webBinding"&gt;
            &lt;security mode="None" /&gt;
          &lt;/binding&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="poxBehavior"&gt;
            &lt;webHttp/&gt;
          &lt;/behavior&gt;
          &lt;behavior name="jsonBehavior"&gt;
            &lt;enableWebScript  /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="defaultBehavior"&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;services&gt;
        &lt;service name="RivWorks.Web.Services.Negotiater" behaviorConfiguration="defaultBehavior"&gt;
          &lt;endpoint address="json"
                    binding="webHttpBinding"
                    bindingConfiguration="webBinding"
                    behaviorConfiguration="jsonBehavior"
                    contract="RivWorks.Web.Services.INegotiaterJSON" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"&gt;
        &lt;baseAddressPrefixFilters&gt;
          &lt;add prefix="http://dev.rivworks.com" /&gt;
        &lt;/baseAddressPrefixFilters&gt;
      &lt;/serviceHostingEnvironment&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p><em>simple test page</em></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
      &lt;script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.js" type="text/javascript"&gt;&lt;/script&gt;
  
      &lt;script language="javascript" type="text/javascript"&gt;
          function sendReq() {
              alert("Before AJAX call");
              $.ajax(
              {
                  type: "POST"
                  , url: "http://dev.rivworks.com/Services/Negotiater.svc/GetSetup"
                  , data: "{ \"ref\":\"http://www.rivworks.com/page.htm\", \"dt\":\"Mon Dec 14 2009 10:45:25 GMT-0700 (MST)\", \"productId\":\"5fea7947-251d-4779-85b7-36796edfe7a3\" }"
                  , contentType: "application/json; charset=utf-8"
                  , dataType: "json"
                  , success: GetMessagesBack
                  , error: Failure
              }
              );
              alert("After AJAX call");
          }
          function GetMessagesBack(data, textStatus) {
              alert(textStatus + "\n" + data);
          }
          function Failure(XMLHttpRequest, textStatus, errorThrown) {
              alert(textStatus + "\n" + errorThrown + "\n" + XMLHttpRequest);
          }
      &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="test"&gt;Bleh&lt;/div&gt;
      &lt;!--&lt;button onclick="javascript:sendReq()"&gt;TEST IT&lt;/button&gt;--&gt;
      &lt;a href="javascript:sendReq()"&gt;Test it&lt;/a&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And now I am getting this error:
  <strong>IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used.</strong></p>
  
  <p>How do I handle this?  &lt;state emotion='wrung out' physical='beat up' /&gt;</p>