Note that there are some explanatory texts on larger screens.

plurals
  1. PO[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    text
    copied!<p>In ASP.net web service if the above isn't specified , what is the response format by default? Also, if my web service below:</p> <pre><code>[WebMethod()] public List&lt;Sample&gt; GenerateSamples(string[][] data) { ResultsFactory f = new ResultsFactory(data); List&lt;Sample&gt; samples = f.GenerateSamples(); return samples; } </code></pre> <p>returns the list of objects, If I change the response format to JSON, I have to change the return type to string, then how do I access objects in my javascript?</p> <p>Currently I call this web service in my JS such as:</p> <pre><code> $.ajax({ type: "POST", url: "http://localhost/TemplateWebService/Service.asmx/GenerateSamples", data: jsonText, contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { var samples = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d; if (samples.length &gt; 0) { doSomethingHere(samples); } else { alert("No samples have been generated"); } }, error: function(xhr, status, error) { var msg = JSON.parse(xhr.responseText); alert(msg.Message); } }); </code></pre> <p>What i noticed though, even though everything works perfectly fine, the eval statement never gets executed, which means that the web service always returns a string!</p> <p>So my question is, is [ScriptMethod(ResponseFormat = ResponseFormat.Json)] necessary on the web service definition side?</p> <p>The way things are now, I can use samples array and access each object and its properties as I normally would in any OOP code, which is very convenient, and everything works no problem, but I just wanted to make sure that I am not missing anything in my set up. </p> <p>I took the basics of combining Jquery's ajax with asp.net from Encosia side, and the response type wasn't mentioned there - I read it on another site and am I not sure how vital it is.</p> <p><a href="http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx" rel="noreferrer">http://www.codeproject.com/KB/webservices/JsonWebServiceJQuery.aspx</a></p> <p>Lists 4 different changes on the asp.net web service side. I only have the first 2 - in my web.config. The service itself and the Sample class is implemented without any serialization, it does have properties though. I guess the web service is JSON by default? And as long as your objects have properties, they are serializable by default? That was my understanding until I read this article.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload