{"id":1201,"date":"2022-08-30T15:14:04","date_gmt":"2022-08-30T15:14:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/making-use-of-json-returned-from-serverside-to-clientside-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:04","modified_gmt":"2022-08-30T15:14:04","slug":"making-use-of-json-returned-from-serverside-to-clientside-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/making-use-of-json-returned-from-serverside-to-clientside-collection-of-common-programming-errors\/","title":{"rendered":"Making use of JSON returned from serverside to clientside-Collection of common programming errors"},"content":{"rendered":"<p><strong>UPDATE 3:<\/strong><\/p>\n<p>The following does not seem to make any difference, does exactly the same thing as the script in update 2:<\/p>\n<pre><code>var $obj = jQuery.parseJSON(results);\n\n$.each($obj, function(key, value) { \n    alert(key + \" - \" + value);\n});\n<\/code><\/pre>\n<p><strong>UPDATE 2:<\/strong><\/p>\n<pre><code>$.each(jQuery.parseJSON(results), function(key, value) { \n    alert(key + \" - \" + value);\n});\n<\/code><\/pre>\n<p>The script above is now returning the currect number of alerts, but not the correct data. it gives me this:<\/p>\n<pre><code>0 - [object, Object]\n1 - [object, Object]\n2 - [object, Object]\n<\/code><\/pre>\n<p><strong>UPDATE 1:<\/strong><\/p>\n<p>The <code>get_json.aspx<\/code> is currently returning this:<\/p>\n<pre><code>[{\"key\":1,\"value\":\"Default\"},{\"key\":2,\"value\":\"Reports\"},{\"key\":3,\"value\":\"Other\"}]\n<\/code><\/pre>\n<p>The current script now looks like this:<\/p>\n<pre><code>$.ajax({\n    url: 'get_json.aspx',\n    type: 'GET',\n    error: function(xhr, status, error) {\n        alert('Error: ' + status + '\\nError Text: ' + error + '\\nResponse Text: ' + xhr.responseText);\n    },\n    success: function(results) {\n        $.each(results, function(key, value) { \n            alert(key + \" - \" + value);\n        });\n    }\n});\n<\/code><\/pre>\n<p>But when I go to this webpage, it just gives me a never ending supply of javascript alerts, i.e.<\/p>\n<pre><code>    0 - undefined\n    1 - undefined\n    2 - undefined\n    3 - undefined\n...\n    75 - undefined\n    76 - undefined\n    77 - undefined\n    78 - undefined\n<\/code><\/pre>\n<p><strong>ORIGINAL QUESTION:<\/strong><\/p>\n<p>If I return key:value paired JSON from the server. How do I make use of that JSON?<\/p>\n<p>I am trying to use the JSON in the success part of the ajax\/jquery below:<\/p>\n<pre><code>$.ajax({\n    url: 'get_json.aspx',\n    type: 'GET',\n    error: function(xhr, status, error) {\n        alert('Error: ' + status + '\\nError Text: ' + error + '\\nResponse Text: ' + xhr.responseText);\n    },\n    success: function(results) {\n    \/*\n    using the key:value pair json return, if 3 pairs are return, somehow generate the following\n    where key = the value of the querystring in the url, i.e. tab = 1,2,3\n    and value = Default, Reports, Other\n        $(\"#tabs\").tabs(\"add\",\"tabs.aspx?tab=1\",\"Default\")\n        $(\"#tabs\").tabs(\"add\",\"tabs.aspx?tab=2\",\"Reports\")\n        $(\"#tabs\").tabs(\"add\",\"tabs.aspx?tab=3\",\"Other\")\n    *\/\n    }\n});\n<\/code><\/pre>\n<ol>\n<li>\n<p>Take a look at http:\/\/www.json.org\/js.html<\/p>\n<\/li>\n<li>\n<p>You would probably want something like this:<\/p>\n<p><strike>$.each(results, function(key, value) { $(&#8220;#tabs&#8221;).tabs(&#8220;add&#8221;,&#8221;tabs.aspx?tab=&#8221; + key,value) });<\/strike><\/p>\n<pre><code>$.each(results, function(i) { \n    $(\"#tabs\").tabs(\"add\",\"tabs.aspx?tab=\" + results[i].key, results[i].value)\n});\n<\/code><\/pre>\n<p>If the content type of the response is &#8220;text\/json&#8221; then you don&#8217;t need to use parseJson() because JQuery will do that for you.<\/p>\n<\/li>\n<li>\n<p>When you return <strong>properly<\/strong> formed json (You can verify that it is properly formed here Json Parser Online). You can use the eval() function supplied by javascript. Ie.<\/p>\n<pre><code>var myJSONObject = {\"bindings\": [\n        {\"ircEvent\": \"PRIVMSG\", \"method\": \"newURI\", \"regex\": \"^http:\/\/.*\"},\n        {\"ircEvent\": \"PRIVMSG\", \"method\": \"deleteURI\", \"regex\": \"^delete.*\"},\n        {\"ircEvent\": \"PRIVMSG\", \"method\": \"randomURI\", \"regex\": \"^random.*\"}\n    ]\n};\n<\/code><\/pre>\n<p>To use a returned text string&#8230; do this:<\/p>\n<pre><code>var myObject = eval('(' + myJSONtext + ')');\n<\/code><\/pre>\n<p>If you insist on using the jquery parsing options, use:<\/p>\n<pre><code>var obj = jQuery.parseJSON( myJsontext ); \n<\/code><\/pre>\n<p>This info is available at JSON in JavaScript<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:09:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>UPDATE 3: The following does not seem to make any difference, does exactly the same thing as the script in update 2: var $obj = jQuery.parseJSON(results); $.each($obj, function(key, value) { alert(key + &#8221; &#8211; &#8221; + value); }); UPDATE 2: $.each(jQuery.parseJSON(results), function(key, value) { alert(key + &#8221; &#8211; &#8221; + value); }); The script above [&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-1201","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1201","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=1201"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1201\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}