{"id":973,"date":"2022-08-30T15:10:16","date_gmt":"2022-08-30T15:10:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/undefined-object-from-php-json-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:16","modified_gmt":"2022-08-30T15:10:16","slug":"undefined-object-from-php-json-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/undefined-object-from-php-json-collection-of-common-programming-errors\/","title":{"rendered":"undefined object from php json-Collection of common programming errors"},"content":{"rendered":"<p>I try to send json array to ajax (from PHP to jQuery)<\/p>\n<p>in my PHP script:<\/p>\n<pre><code>$value = 'test';\necho json_encode(\n    array(\n        'key' =&gt; $value\n    )\n);\nexit;\n<\/code><\/pre>\n<p>Javascript side:<\/p>\n<pre><code>function sweety_ajax(url, datas)\n{\n    $.ajax({\n        url: url,\n        type: 'POST',\n        cache: false,\n        dataType: 'json',\n        data: datas,\n        success: function(r){\n            return r;\n        }\n    });\n}\n<\/code><\/pre>\n<p>I get the correct value with r.key inside the function, but I would like to get my &#8220;r&#8221; object like this:<\/p>\n<pre><code>var response = sweety_ajax(url, datas);\n\nalert(response.key);\n<\/code><\/pre>\n<p>But the problem is that response object is undefined and cannot get the value of key&#8230;<\/p>\n<p>Any idea?<\/p>\n<ol>\n<li>\n<p>AJAX request in asynchronous. You need to pass a callback if you want to do this.<\/p>\n<pre><code>function sweety_ajax(url, datas, callback)\n{\n    $.ajax({\n        url: url,\n        type: 'POST',\n        cache: false,\n        dataType: 'json',\n        data: datas,\n        success: callback\n    });\n}\n\nsweety_ajax(url, datas, function(response) {\n  alert(response.key);\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:09:06. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I try to send json array to ajax (from PHP to jQuery) in my PHP script: $value = &#8216;test&#8217;; echo json_encode( array( &#8216;key&#8217; =&gt; $value ) ); exit; Javascript side: function sweety_ajax(url, datas) { $.ajax({ url: url, type: &#8216;POST&#8217;, cache: false, dataType: &#8216;json&#8217;, data: datas, success: function(r){ return r; } }); } I get the [&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-973","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/973","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=973"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/973\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}