Note that there are some explanatory texts on larger screens.

plurals
  1. POsending JSON object successfully to asp.net WebMethod, using jQuery
    text
    copied!<p>I've been working on this for 3 hours and have given up. i am simply trying to send data to an asp.net web method, using jQuery. The data is basically a bunch of key/value pairs. so i've tried to create an array and adding the pairs to that array.</p> <p>My WebMethod(aspx.cs) looks like this (this may be wrong for what i'm building in javascript, i just dont know): </p> <pre><code> [WebMethod] public static string SaveRecord(List&lt;object&gt; items) ..... </code></pre> <p>Here is my sample javascript:</p> <blockquote> <p>var items = new Array;</p> <pre><code> var data1 = { compId: "1", formId: "531" }; var data2 = { compId: "2", formId: "77" }; var data3 = { compId: "3", formId: "99" }; var data4 = { status: "2", statusId: "8" }; var data5 = { name: "Value", value: "myValue" }; items[0] = data1; items[1] = data2; items[2] = data3; items[3] = data4; items[4] = data5; </code></pre> </blockquote> <pre><code>Here is my jQuery ajax call: var options = { error: function(msg) { alert(msg.d); }, type: "POST", url: "PackageList.aspx/SaveRecord", data: { 'items': items }, contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function(response) { var results = response.d; } }; jQuery.ajax(options); </code></pre> <p>i get the error -<code>Invalid JSON primitive: items.</code>-</p> <p>so...if i do this:</p> <blockquote> <p>var DTO = { 'items': items };</p> </blockquote> <p>and set the data parameter like this: </p> <blockquote> <p>data: JSON.stringify(DTO)</p> </blockquote> <p>then i get this error:</p> <pre><code>Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.List`1[System.Object]\u0027 </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