{"id":1242,"date":"2022-08-30T15:14:45","date_gmt":"2022-08-30T15:14:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/accessing-the-parameters-of-a-json-call-in-the-web-method-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:45","modified_gmt":"2022-08-30T15:14:45","slug":"accessing-the-parameters-of-a-json-call-in-the-web-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/accessing-the-parameters-of-a-json-call-in-the-web-method-collection-of-common-programming-errors\/","title":{"rendered":"accessing the parameters of a JSON call in the web method-Collection of common programming errors"},"content":{"rendered":"<p>I have the following JSON call, the data I&#8217;m passing seems to be getting stringify&#8217;ed properly from what i&#8217;m looking at, however, I don&#8217;t seem to have the right syntax to process the parameter in the public web method.<\/p>\n<p>Here is the JSON call:<\/p>\n<pre><code> \n\n  var qs = new Querystring();\n\n  var v1 = qs.get(\"TorVName\");\n\n  var jsonData = JSON.stringify(v1);  \n\n        $().ready(function() {\n            $.ajax({\n                type: \"POST\",\n                url: \"Default.aspx\/GetColumns\",\n                data: jsonData,\n                contentType: \"application\/json; charset=utf-8\",\n                dataType: \"json\",\n                success: function(msg) {\n       var optString = 'Select Column';\n        $.each(msg.d, function(index, item) {\n        optString += '' + item + '';\n        });\n        $('select[name^=DDLColumns]').html(optString);\n                },\n                error: function() {\n                    alert(\"Failed to load columns\");\n                }\n            });\n        });\n\n<\/code><\/pre>\n<p>and here is the cooresponding web method:<\/p>\n<pre><code>    [WebMethod]\n    public static ArrayList GetColumns(string TorVName)\n    {\n        String cnstr = \"myconnect string\";\n        string Sql = String.Empty;\n        ArrayList arlist = new ArrayList();\n        SqlDataReader rdr = null;\n        SqlCommand cmd = null;\n        DataSet dset;\n        SqlConnection cn = new SqlConnection(cnstr);\n        cn.Open();\n        dset = new DataSet(\"ds\");\n        dset.Clear();\n<\/code><\/pre>\n<p>etc etc&#8230;<\/p>\n<p>I have a hard time deciding how to debug a web method since I can only see the client side actions in firebug.<\/p>\n<p>any help on how to recieve and process the parameter in the web method would be most appreciated.<\/p>\n<p>Thanks Deano<\/p>\n<ol>\n<li>\n<p>Your data needs to be in name\/value pair format, like this:<\/p>\n<pre><code> var jsonData = JSON.stringify({ TorVName: qs.get(\"TorVName\") }); \n<\/code><\/pre>\n<p>The web method is looking for the property named <code>TorVName<\/code>, so you need a name\/value pair with this for your JSON request, not <em>just<\/em> the string. In the above code the <code>TorVName:<\/code> is because that&#8217;s what the parameter is named in the web method.<\/p>\n<\/li>\n<li>\n<p>as others have stated, you need ot have a json object with a parameter of torVName since htat is what your web method is looking for. an easy way to do this is var data={};\/\/create new object data[&#8216;torVName&#8217;]=actual datta&#8217;; then in your ajax call you can just say data:JSON.stringify(data)<\/p>\n<\/li>\n<li>\n<p>maybe you guys should read this before suggesting the stringify stuff in IE8:<\/p>\n<p>JSON returns &#8216;undefined&#8217; when stringifying an object that was created in a different window See test case at http:\/\/kinsey.no\/examples\/ie8_json_bug\/<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:47. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have the following JSON call, the data I&#8217;m passing seems to be getting stringify&#8217;ed properly from what i&#8217;m looking at, however, I don&#8217;t seem to have the right syntax to process the parameter in the public web method. Here is the JSON call: var qs = new Querystring(); var v1 = qs.get(&#8220;TorVName&#8221;); var jsonData [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1242"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1242\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}