{"id":1723,"date":"2022-08-30T15:18:54","date_gmt":"2022-08-30T15:18:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/29\/jquery-json-cannot-convert-data-to-object-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:54","modified_gmt":"2022-08-30T15:18:54","slug":"jquery-json-cannot-convert-data-to-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/jquery-json-cannot-convert-data-to-object-collection-of-common-programming-errors\/","title":{"rendered":"jQuery\/Json &#8211; Cannot convert &#39;data&#39; to object-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve a simple ajax\/json request with jQuery:<\/p>\n<pre><code>$.ajax({\n              type: \"POST\",\n              url: \"\/some-json-url\",\n              data: \"score=\" + 1,\n              dataType: 'json',\n              success: function(data){\n                if(data.msg){ \n                    alert(data.msg);\n                }\n             }\n        });\n<\/code><\/pre>\n<p>However, if the msg is not set, I gen an error (looking through Opera Dragonfly):<\/p>\n<p><strong>Unhandled Error: Cannot convert &#8216;data&#8217; to object<\/strong><\/p>\n<p>How can I check if it exists or not&#8230; in a valid way?<\/p>\n<ol>\n<li>\n<p>If the problem is with <code>data<\/code> being null then you can check it like so:<\/p>\n<pre><code>if(data &amp;&amp; data.msg){\n    \/\/...\n}\n<\/code><\/pre>\n<p>or if you have multiple properties, either like this:<\/p>\n<pre><code>if(data){\n    if(data.msg){\n        \/\/...\n    }\n}\n<\/code><\/pre>\n<p>or return early:<\/p>\n<pre><code>if(!data)\n    return;\n\nif(data.msg){\n    \/\/...\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-29 06:17:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve a simple ajax\/json request with jQuery: $.ajax({ type: &#8220;POST&#8221;, url: &#8220;\/some-json-url&#8221;, data: &#8220;score=&#8221; + 1, dataType: &#8216;json&#8217;, success: function(data){ if(data.msg){ alert(data.msg); } } }); However, if the msg is not set, I gen an error (looking through Opera Dragonfly): Unhandled Error: Cannot convert &#8216;data&#8217; to object How can I check if it exists or [&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-1723","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1723","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=1723"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1723\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}