Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you use ASMX services the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow noreferrer">JavaScriptSerializer</a> will be responsible for the data conversion and not the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx" rel="nofollow noreferrer">DataContractJsonSerializer</a>. So all <code>DataContract</code> attributes which you use <strong>will not work</strong>.</p> <p>You are write that classes like <code>public class TestCol : List&lt;Test&gt; { }</code> are bad for the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow noreferrer">JavaScriptSerializer</a>, but classes having <code>List&lt;Test&gt;</code> as the property (<code>public class MainTest { public List&lt;Test&gt; Components { get; set; }}</code>) have no problem.</p> <p>So I suggest to simplify you code to the following. The classes used as the parameters can be defines as</p> <pre><code>public class Test { public String Name { get; set; } } public class MainTest { public List&lt;Test&gt; Components { get; set; } } </code></pre> <p>The WebMethod <code>Test</code> will be</p> <pre><code>[WebMethod] public String Test(MainTest input) { StringBuilder rtrn = new StringBuilder(); foreach (Test test in input.Components) { rtrn.AppendLine (test.Name); } return rtrn.ToString (); } </code></pre> <p>and the ajax call can be</p> <pre><code>var tests = { Components: [ {Name:"Test1"}, {Name:"Test2"}, {Name:"Test3"} ] }; $.ajax({ type: "POST", url: "WebService1.asmx/Test", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify({ "input": tests }), success: function (data, textStatus) { alert("success:\n" + data.d); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText+" || "+textStatus+" || "+errorThrown); } }); </code></pre> <p>How you can see all will be very simple and it's work. For more details how you can send complex data I recommend you to read <a href="https://stackoverflow.com/questions/3445859/asmx-web-service-json-javascript-jquery/3446517#3446517">another answer</a> and <a href="https://stackoverflow.com/questions/2737525/how-do-i-build-a-json-object-to-send-to-an-ajax-webservice/2738086#2738086">this</a>.</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