{"id":289,"date":"2011-04-18T11:54:51","date_gmt":"2011-04-18T06:24:51","guid":{"rendered":"http:\/\/www.create-dynamic.com\/blog\/?p=289"},"modified":"2015-10-07T15:33:22","modified_gmt":"2015-10-07T10:03:22","slug":"ajax-json-jquery","status":"publish","type":"post","link":"https:\/\/www.create-dynamic.com\/blog\/289\/phptags\/ajax-json-jquery\/","title":{"rendered":"Ajax Json jquery"},"content":{"rendered":"<p>How to convert the json obj into something<br \/>\n<strong>AJAX<\/strong> (Asynchronous JavaScript and XML).Request the data to server after page loads and get response.This is asynchronous.With AJAX we can change the particular content in a loaded page.<\/p>\n<p><strong>JSON <\/strong>(JavaScript Object Notation) is a lightweight data-interchange format. It is one of the best method to exchange data like XML.<br \/>\n<strong>jQuery <\/strong>is a java script library provides set of method to handle request and response.With jQuery we can request AJAX call to a server.<br \/>\n<strong>Server Script[server.php]<\/strong><\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">echo '{1:\"PHP\",2:\"MYSQL\",3:\"AJAX\",4:\"JAVASCRIPT\",5:\"JSON\"}';<\/code><\/div><strong>Client Side Script:[test.php]<\/strong><div class=\"div_code\"><pre><code class=\"js\">$.ajax({\r\n   type: \"POST\",\r\n   url: \"server.php\",\r\n   data: \"website=\"+website,\r\n   success: function(resp){\r\n\t\/\/alert(resp);\r\n\teval(\"var response = \" + resp);\r\n\tif(response!='')\t{\t\r\n\t\/\/alert(response[1]);  \/\/ Return PHP\r\n\t\/\/alert(response[2]);  \/\/ Return MYSQL\r\n\tfor(jsonloop in response)\r\n\t\talert(jsonloop+\"-\"+response[jsonloop]);\t\t\r\n\t}\r\n   },\r\n   error: function(e){\r\n     alert('Error: ' + e);\r\n   }\r\n });\r\n<br>\r\n<\/code><\/pre>\n<\/div>\n<h2>$.getJSON Method<\/h2>\n<p><strong>Server Script[server.php]<\/strong><br \/>\nBelow are the php server script with json format about books<\/p>\n<div class=\"div_code\">\n<pre><code class=\"php\">\/\/header('Content-type: text\/json');\t\r\nheader('Content-type: application\/json');\r\necho '{\"title\": \"Learning Jquery\",<br>\"date_published\": \"June 2007\",<br>\"description\": \"Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file\",<br>\"ISBN\": \"978-1-847192-50-9\",<br>\"author\": \"Jonathan chaffer and Karl swedberg\",<br>\"tags\": \"Jquery Javascript design js framework\"}';<\/code><\/pre>\n<\/div>\n<p><strong>Client Side Script:[test.php]<\/strong><br \/>\nBelow are the client side code<\/p>\n<div class=\"div_code\">\n<pre><code class=\"js\"><strong>$.getJSON<\/strong>(\"http:\/\/localhost\/server.php\",<br>\r\n        function(data){$.each(data,function(i){<br>\t\t\r\n\t\t\t$(\"#json\").append(\"< br \/>\"+i+\":\"+data[i]);<br>\t\t\r\n\t\t})\r\n});\r\n<\/code><\/pre>\n<\/div>\n<p>Output would be<\/p>\n<div class=\"div_code\">\n<pre><code class=\"text\">title:Learning Jquery\r\ndate_published:June 2007<br>description:Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file\r\nISBN:978-1-847192-50-9\r\nauthor:Jonathan chaffer and Karl swedberg<br>tags:Jquery Javascript design js framework<\/code><\/pre>\n<\/div>\n<h2>$.ajax Method [Both GET and POST without datatype]<\/h2>\n<p><strong>Client Side Script:[test.php]<\/strong><br \/>\nBelow are the client side code.When using ajax method, JSON response returned and stored in data variable. But this variable become strings. So using eval method make it to objects<\/p>\n<div class=\"div_code\">\n<pre><code class=\"js\">$.ajax({\r\nurl: \"http:\/\/localhost\/testing\/js\/jquery\/ajaxjson_server.php\",\r\ntype: 'GET',\r\ndata: \"sid=\"+Math.random(),\r\nsuccess: function(data) {\r\n<strong>eval(\"data=\"+data);<\/strong>\r\n$.each(data,function(i){\r\n$(\"#book\").append(\"< br\/ >\"+i+\":\"+data[i]);\r\n})\r\n}\r\n});<\/code><\/pre>\n<\/div>\n<h2>$.ajax Method [Both GET and POST with datatype]<\/h2>\n<p><strong>Client Side Script:[test.php]<\/strong><br \/>\nBelow are the client side code.When using ajax method, JSON response returned and stored in data variable.When using datatype as json, it parses into object.So no need to use eval function. <\/p>\n<div class=\"div_code\">\n<pre><code class=\"js\">$.ajax({\r\nurl: \"http:\/\/localhost\/testing\/js\/jquery\/ajaxjson_server.php\",\r\ntype: 'GET',\r\n<strong>dataType: 'json'<\/strong>,\r\ndata: \"sid=\"+Math.random(),\r\nsuccess: function(data) {\r\n$.each(data,function(i){\r\n$(\"#book\").append(\"< br\/ >\"+i+\":\"+data[i]);\r\n})\r\n}\r\n});<br><\/code><\/pre>\n<\/div>\n<p>Output would be<\/p>\n<div class=\"div_code\">\n<pre><code class=\"text\">title:Learning Jquery\r\ndate_published:June 2007<br>description:Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file<br>ISBN:978-1-847192-50-9\r\nauthor:Jonathan chaffer and Karl swedberg<br>tags:Jquery Javascript design js framework<br>\r\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to convert the json obj into something AJAX (Asynchronous JavaScript and XML).Request the data to server after page loads and get response.This is asynchronous.With AJAX we can change the particular content in a loaded page. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is one of the best method to exchange data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23,1],"tags":[34,36,33,37,35],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-javascript","category-jquery","category-phptags","tag-ajax","tag-ajax-and-jquery","tag-ajax-json","tag-javascript-2","tag-jquery-2"],"_links":{"self":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":12,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.create-dynamic.com\/blog\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}