{"id":953,"date":"2013-02-05T14:30:25","date_gmt":"2013-02-05T14:30:25","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=953"},"modified":"2022-02-17T07:14:46","modified_gmt":"2022-02-17T07:14:46","slug":"ajax-request-for-text","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/ajax-javascript\/ajax-request-for-text","title":{"rendered":"AJAX Request For Text"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nA Working&nbsp;AJAX Request<br \/>\n<\/h4>\n<h5>\nThe HTML \/ Javascript<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n&lt;!DOCTYPE html&gt; \r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot;&gt;\r\n\r\n&lt;script&gt;\r\n\/\/***********************************\r\n\/\/***********************************\r\n\/\/********** AJAX HANDLERS **********\r\n\/\/***********************************\r\n\/\/***********************************\r\n\r\n\/\/&lt;div id=&#39;ajax_area1&#39;&gt;THIS WILL BE REPLACED&lt;\/div&gt;\r\n\r\n\/\/Setup to make an HTTP POST Ajax request\r\nAjaxRequest1Parameters = &quot;request_type=get_image_to_show&quot;;\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET PARAMETER TO POST  (id1=val1&amp;id2=val2 for multiple parameters)\r\nAjaxRequest1 = new ajaxRequest();\r\nAjaxRequest1.open(&quot;POST&quot;, &quot;ajax.php&quot;, true);\t\t\t\t\t\t\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET THE FILE TO POST THE REQUEST TO\r\nAjaxRequest1.setRequestHeader(&quot;Content-type&quot;, &quot;application\/x-www-form-urlencoded; charset=UTF-8&quot;);\r\nAjaxRequest1.setRequestHeader(&quot;Content-length&quot;, AjaxRequest1Parameters.length);\r\nAjaxRequest1.setRequestHeader(&quot;Connection&quot;, &quot;close&quot;);\r\nAjaxRequest1.onreadystatechange = function()\r\n{\r\n\t\/\/-----------------------------------------------\r\n\t\/\/----- RESPONSE RECEIVED FROM AJAX REQUEST -----\r\n\t\/\/-----------------------------------------------\r\n\tif (this.readyState == 4)\t\t\t\t\/\/4=Completed Ajax request\r\n\t{\r\n\t\tif (this.status == 200)\t\t\t\t\/\/200=Call succeeded\r\n\t\t{\r\n\t\t\tif (this.responseText != null)\t\/\/Check we got some response data (this.responseText or this.responseXML)\r\n\t\t\t{\r\n\t\t\t\t\/\/----- PROCESS THE RESPONSE -----\r\n\t\t\t\tdocument.getElementById(&#39;ajax_area1&#39;).innerHTML = this.responseText\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\talert(&quot;Ajax error: No data received&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\talert( &quot;Ajax error: &quot; + this.statusText);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\/\/SEND THE AJAX REQUEST\r\nAjaxRequest1.send(AjaxRequest1Parameters);\t\t\t\/\/Use (null) if there are no parameters to send\r\n\r\n\r\n\/\/************************************************\r\n\/\/************************************************\r\n\/\/********** CREATE AJAX REQUEST OBJECT **********\r\n\/\/************************************************\r\n\/\/************************************************\r\nfunction ajaxRequest()\r\n{\r\n\t\/\/Cross browser - deal with IE varients\r\n\ttry\r\n\t{\r\n\t\t\/\/Non IE Browser\r\n\t\tvar request = new XMLHttpRequest();\r\n\t}\r\n\tcatch(e1)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/IE6?\r\n\t\t\trequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);\r\n\t\t}\r\n\t\tcatch(e2)\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\t\/\/IE5?\r\n\t\t\t\trequest = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);\r\n\t\t\t}\r\n\t\t\tcatch(e3)\r\n\t\t\t{\r\n\t\t\t\t\/\/There is no Ajax support in this browser\r\n\t\t\t\trequest = false;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn request;\r\n}\r\n&lt;\/script&gt;\r\n\r\n\r\n&lt;\/head&gt; \r\n&lt;body&gt; \r\n&lt;div id=&#39;ajax_area1&#39;&gt;THIS WILL BE REPLACED&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<h5>\nThe PHP File<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n&lt;?php\r\nheader(&#39;Content-Type: text\/xml&#39;);\t\t\t\t\t\t\t\t\t\t\t\/\/Include these headers at the very start - they are essential to stop some browsers caching the ajax response\r\nheader(&quot;Cache-Control: no-cache, must-revalidate&quot;);\r\nheader(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;);\t\t\t\/\/A date in the past\r\n\r\nif (isset($_POST['request_type']))\r\n{\r\n\tif ($_POST['request_type'] == &quot;get_image_to_show&quot;)\r\n\t{\r\n\t\t\/\/--------------------------------------------\r\n\t\t\/\/----- AJAX REQUEST - GET IMAGE TO SHOW -----\r\n\t\t\/\/--------------------------------------------\r\n\t\techo &quot;WITH THIS!&quot;;\r\n\t}\r\n}\r\n\r\n?&gt;\r\n<\/code><\/pre>\n<p>\n<em><strong>Note&nbsp;<\/strong><\/em>&#8211; you can&#39;t just use &quot;AjaxRequest1.send(AjaxRequest1Parameters);&quot; again after first setting it up, but you can put the whole thing (including the&nbsp;onreadystatechange&nbsp;function) inside a function and call it whenever you want to send the same&nbsp;ajax&nbsp;request.\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; A Working&nbsp;AJAX Request The HTML \/ Javascript &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=&quot;utf-8&quot;&gt; &lt;script&gt; \/\/*********************************** \/\/*********************************** \/\/********** AJAX HANDLERS ********** \/\/*********************************** \/\/*********************************** \/\/&lt;div id=&#39;ajax_area1&#39;&gt;THIS WILL BE REPLACED&lt;\/div&gt; \/\/Setup to make an HTTP POST Ajax request AjaxRequest1Parameters = &quot;request_type=get_image_to_show&quot;; \/\/&lt;&lt;&lt;&lt;&lt;SET PARAMETER TO POST (id1=val1&amp;id2=val2 for multiple parameters) AjaxRequest1 = new ajaxRequest(); AjaxRequest1.open(&quot;POST&quot;, &quot;ajax.php&quot;, true); \/\/&lt;&lt;&lt;&lt;&lt;SET [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-ajax-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":1348,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/953\/revisions\/1348"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}