c# - JSON-data to WebMethod using jQuery returning errors -
i'm trying send json list populated id's 'data-seq' attribute when 'value' == true.
i have tried out lot solution keeps getting me error messages, common "there no parameterless constructor type string" when using string[] or "string not supported deserialization of array" when using string code-behind parameter in webmethod.
function sentdata() { var json = []; $('.btn[value="true"]').each(function () { var obj = { id: $(this).attr("data-seq") }; json.push(obj); }); json = json.stringify({ jsonlist: json }); console.log(json); // {"jsonlist":[{"id":"38468"},{"id":"42443"},{"id":"42444"}]} (the right id's getting stored) $.ajax({ type: "post", async: true, url: "default.aspx/getlist", datatype: "json", data: json, contenttype: "application/json; charset=utf-8", error: function (jqxhr, textstatus, errorthrown) { console.log('bad, ' + errorthrown + ", " + jqxhr.responsetext + ", " + textstatus); }, success: function(json){ //do result } }); return false; } // code-behind [webmethod] [scriptmethod(usehttpget = false)] public static void getlist(string jsonlist) { // string: string not supported deserialization of array // string[]: there no parameterless constructor type string }
you sending ids send them comma-separated string this:
function sentdata() { var json = []; $('.btn[value="true"]').each(function () { var id = $(this).attr("data-seq") json.push(id); }); $.ajax({ type: "post", async: true, url: "default.aspx/getlist", datatype: "json", data: '{jsonlist: "' + json + '" }', contenttype: "application/json; charset=utf-8", error: function (jqxhr, textstatus, errorthrown) { console.log('bad, ' + errorthrown + ", " + jqxhr.responsetext + ", " + textstatus); }, success: function(json){ //do result } }); return false; }
and code-behind:
[webmethod] [scriptmethod(usehttpget = false)] public static void getlist(string jsonlist) { list<string> listofids = jsonlist.split(',').tolist(); }
Comments
Post a Comment