Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When using AJAX.NET I always make the input parameter just a plain old object and then use the javascript deserializer to covert it to whatever type I want. At least that way you can debug and see what type of object the web method in is recieving.</p> <p>You need to convert your object to a string when using jQuery</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 runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"&gt; &lt;Scripts&gt; &lt;asp:ScriptReference Path="~/js/jquery.js" /&gt; &lt;/Scripts&gt; &lt;/asp:ScriptManager&gt; &lt;div&gt;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;script type="text/javascript" language="javascript"&gt; var items = [{ compId: "1", formId: "531" }, { compId: "2", formId: "77" }, { compId: "3", formId: "99" }, { status: "2", statusId: "8" }, { name: "Value", value: "myValue"}]; //Using Ajax.Net Method PageMethods.SubmitItems(items, function(response) { var results = response.d; }, function(msg) { alert(msg.d) }, null); //using jQuery ajax Method var options = { error: function(msg) { alert(msg.d); }, type: "POST", url: "WebForm1.aspx/SubmitItems", data: {"items":items.toString()}, // array to string fixes it * contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function(response) { var results = response.d; } }; jQuery.ajax(options); &lt;/script&gt; </code></pre> <p>And the Code Behind</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Serialization; using System.Web.Script.Services; using System.Web.UI; using System.Web.UI.WebControls; namespace CustomEquip { [ScriptService] public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static void SubmitItems(object items) { //break point here List&lt;object&gt; lstItems = new JavaScriptSerializer().ConvertToType&lt;List&lt;object&gt;&gt;(items); } } } </code></pre>
 

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