title: Returning JSON from ASMX, and handling it correctly in Javascript
body: <p>I realize there are tonnes of similar questions already up here but I cannot figure this one out.  </p>
  
  <p>I have a Web Service (C#, .net 3.5).  The essential Code you need to know of is as follows:</p>
  
  <pre><code>[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.Web.Script.Services.ScriptService]
  public class WSMember : System.Web.Services.WebService {
  
      public WSMember () {   
      }
  
  
      [WebMethod]
      [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
      public string GetMember(string IdMember)
      {
          //Ignore the parameter for now... I will be looking up a database with it... 
          //For now just return a minimal object:
          Member m = new Member();
          m.Surname = "Smith";
          m.FirstName = "John";
          return new JavaScriptSerializer().Serialize(m);
      }
  </code></pre>
  
  <p>Also, in web.config, I made the following addition (which I just saw on some other post... is this normal/safe?)</p>
  
  <pre><code>  &lt;webServices&gt;
        &lt;protocols&gt;
          &lt;add name="HttpGet" /&gt;
          &lt;add name="HttpPost" /&gt;
        &lt;/protocols&gt;
      &lt;/webServices&gt;
  </code></pre>
  
  <p>Then in Default.aspx, I the two key references...</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
  &lt;script type="text/javascript" src="jquery.json-2.2.min.js"  &gt;&lt;/script&gt;
  </code></pre>
  
  <p>jquery.json-2.2.min.js was downloaded from google code</p>
  
  <p>And Here is the Javascript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
           $(document).ready(function() {
               $("#button1").click(function(event) {
                   var myData = { IdMember: "2" };
                   var encoded = $.toJSON(myData);
  
                   alert(encoded);
  
                   $.ajax({
                       type: "POST",
                       url: "WSMember.asmx/GetMember",
                       data: encoded,
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function(msg) {
                           alert("worked" + msg.d);
                           //$("#sidebar").append(msg);
                       },
                       error: function(msg) {
                           alert(msg.d);
                           //$("#sidebar").append(msg);
                       }
                   });
               });
           });
  
      &lt;/script&gt;
  </code></pre>
  
  <p>When I execute it, the encoded json appears in the message box as expected... i.e. with double quotes:</p>
  
  <p>{ "IdMember":"2" }</p>
  
  <p>However, it always fails.  Even for the most basic Hello World with no data being passed in, it fails.  I keep getting "undefined" for the message data.</p>
  
  <p>If I just use alert(msg), it displays [object XMLHttpRequest]  </p>
  
  <p>Does anyone know where my data is getting lost??  </p>
  
  <p>And another question... is there anything fundamentally wrong with what I'm doing?</p>
  
  <p>Thanks a lot.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>thanks for the reply guys.  I have tried the following so... </p>
  
  <p>UseHttpGet = true is now changed to false.  (Again - I saw it somewhere so I tried it... but I knew it couldn't be right :-/ )</p>
  
  <p>Let's say the web service now returns a string.  I build the string as follows (seems a bit crazy... serializing it did the exact same thing... )</p>
  
  <pre><code>    StringBuilder sb = new StringBuilder();
      sb.Append("{");
      sb.Append("\"Surname\":");
      sb.Append("\"");
      sb.Append(m.Surname);
      sb.Append("\"");
  
      sb.Append(",\"FirstName\":");
      sb.Append("\"");
      sb.Append(m.FirstName);
      sb.Append("\"");
  
      sb.Append("}");
  
      return sb.ToString();
  </code></pre>
  
  <p>This code returns something like:</p>
  
  <pre><code>{"Surname":"Smith","FirstName":"John"}
  </code></pre>
  
  <p>I still get the exact same error... </p>
  
  <p>I have also tried something like returning the object "Member",so the code becomes:</p>
  
  <pre><code>[WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public Member GetMember(string IdMember)
  {
      Member m = new Member();
      m.Surname = "Smith";
      m.FirstName = "John";
  
      return m;
  }
  </code></pre>
  
  <p>This too throws the same error.</p>
  
  <p>Sorry to be a pain... I've read both of those links, and others.  Just can't see why this any different.</p>
  
  <p>Is there any extra config setting I need to be aware of possibly??</p>
  
  <p>Thanks a lot for replies.</p>
  
  <p><strong>UPDATE:</strong>
  Problem is fixed.  The key mistakes in the above code are:</p>
  
  <pre><code>[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
  </code></pre>
  
  <p>should be</p>
  
  <pre><code>[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  </code></pre>
  
  <p>Also, on the form, when using a button to call the javascript, I was incorrectly setting the input type... </p>
  
  <pre><code>&lt;input id="button1" type="submit" value="Just a test" /&gt;
  </code></pre>
  
  <p>when it <strong>should</strong> say:</p>
  
  <pre><code>&lt;input id="button1" type="button" value="Just a test" /&gt;
  </code></pre>
  
  <p>Many thanks to all who helped.</p>