{"id":966,"date":"2013-02-05T16:27:01","date_gmt":"2013-02-05T16:27:01","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=966"},"modified":"2022-02-17T07:14:46","modified_gmt":"2022-02-17T07:14:46","slug":"ajax-request-for-xml-values","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/ajax-javascript\/ajax-request-for-xml-values","title":{"rendered":"AJAX Request For XML Values"},"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;script&gt;\r\n\/\/Setup to make an HTTP POST Ajax request\r\nAjaxRequest1Parameters = &quot;request_type=get_xml_values&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.responseXML != 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\tvar xmlDoc = AjaxRequest1.responseXML.documentElement;\r\n\t\t\t\tdocument.getElementById(&quot;ajax_area1&quot;).innerHTML = xmlDoc.getElementsByTagName(&quot;ajax_area1&quot;)[0].childNodes[0].nodeValue;\r\n\t\t\t\tdocument.getElementById(&quot;ajax_area2&quot;).innerHTML = xmlDoc.getElementsByTagName(&quot;ajax_area2&quot;)[0].childNodes[0].nodeValue;\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&lt;\/script&gt;\r\n\r\n&lt;div id=&#39;ajax_area1&#39;&gt;THIS WILL BE REPLACED&lt;\/div&gt;&lt;br \/&gt;\r\n&lt;div id=&#39;ajax_area2&#39;&gt;THIS WILL ALSO BE REPLACED&lt;\/div&gt;&lt;br \/&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\n\r\nif (isset($_POST['request_type']))\r\n{\r\n\tif ($_POST['request_type'] == &quot;get_xml_values&quot;)\r\n\t{\r\n\t\t\/\/-----------------------------------------\r\n\t\t\/\/----- AJAX REQUEST - GET XML VALUES -----\r\n\t\t\/\/-----------------------------------------\r\n\t\t\/\/echo &quot;&lt;img src=\\&quot;images\/slider_led_red.png\\&quot; width=\\&quot;22\\&quot; height=\\&quot;8\\&quot;&gt;&quot;;\r\n\t\t\/\/echo &quot;WITH THIS!&quot;;\r\n\t\t\r\n\t\techo &quot;&lt;?xml version=&#39;1.0&#39; encoding=&#39;ISO-8859-1&#39;?&gt;&quot;;\r\n\t\techo &quot;&lt;group&gt;&quot;;\r\n\t\techo &quot;\t&lt;ajax_area1&gt;WITH THIS&lt;\/ajax_area1&gt; &quot;;\r\n\t\techo &quot;\t&lt;ajax_area2&gt;AND THIS&lt;\/ajax_area2&gt; &quot;;\r\n\t\techo &quot;&lt;\/group&gt;&quot;;\r\n\t}\r\n}\r\n\r\n?&gt;\r\n<\/code><\/pre>\n<p>\n<em><strong>Note <\/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 function) inside a function and call it whenever you want to send the same ajax request.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; A Working&nbsp;AJAX Request The HTML \/ Javascript &lt;script&gt; \/\/Setup to make an HTTP POST Ajax request AjaxRequest1Parameters = &quot;request_type=get_xml_values&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 THE FILE TO POST THE REQUEST TO AjaxRequest1.setRequestHeader(&quot;Content-type&quot;, &quot;application\/x-www-form-urlencoded; charset=UTF-8&quot;); AjaxRequest1.setRequestHeader(&quot;Content-length&quot;, AjaxRequest1Parameters.length); AjaxRequest1.setRequestHeader(&quot;Connection&quot;, &quot;close&quot;); AjaxRequest1.onreadystatechange = function() { \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; [&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-966","post","type-post","status-publish","format-standard","hentry","category-ajax-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/966","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=966"}],"version-history":[{"count":13,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/966\/revisions"}],"predecessor-version":[{"id":1344,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/966\/revisions\/1344"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}