Page 1 of 1

Understanding a function

Posted: Sun Sep 28, 2014 8:44 am
by gautamz07
below is the code :

Code: Select all

  function createXHR()
		{
			try { return new XMLHttpRequest(); } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
			alert("XMLHttpRequest not supported");
			return null;
		}
		function sendRequest()
		{
			var xhr = createXHR(); // cross browser XHR creation
				if (xhr) // if created run request
				{
					xhr.open("GET","sayhello.php",true);
					xhr.onreadystatechange = function(){handleResponse(xhr);};
					xhr.send(null);
				}
		}

		function handleResponse(xhr){
			if (xhr.readyState == 4 && xhr.status == 200)
			{
			var parsedResponse = xhr.responseXML;
			var msg = parsedResponse.getElementsByTagName("message")[0].firstChild.nodeValue;
			var responseOutput = document.getElementById("responseOutput");
			responseOutput.innerHTML = msg;
			}
		}

		window.onload = function ()
		{
		document.getElementById("helloButton").onclick = sendRequest;
		};
I have a problem understanding this function , check the code .

Code: Select all

     		function handleResponse(xhr){
			if (xhr.readyState == 4 && xhr.status == 200)
			{
			var parsedResponse = xhr.responseXML;
			var msg = parsedResponse.getElementsByTagName("message")[0].firstChild.nodeValue;
			var responseOutput = document.getElementById("responseOutput");
			responseOutput.innerHTML = msg;
			}
		}
i Don't get what the below line is doing in the code ? , is it an absolute must ???

Code: Select all

   var parsedResponse = xhr.responseXML;

Re: Understanding a function

Posted: Sun Sep 28, 2014 11:13 am
by Celauran
It's saving a property of the xhr object to a local variable.

Re: Understanding a function

Posted: Sun Sep 28, 2014 6:49 pm
by twinedev
More specifically, it is taking the response from the AJAX request and converting it to XML format and then assigning that to parsedResponse

Re: Understanding a function

Posted: Mon Sep 29, 2014 2:04 pm
by gautamz07
twinedev wrote:More specifically, it is taking the response from the AJAX request and converting it to XML format and then assigning that to parsedResponse
Why is it converting it to XML ????

Re: Understanding a function

Posted: Tue Sep 30, 2014 6:02 am
by gautamz07
Or is it converting The PHP response to XML or the JS to XML .. I am confused :o

Re: Understanding a function

Posted: Wed Oct 01, 2014 1:49 am
by twinedev
Why? Well that is just what the function does. I have never used it before, but my guess would be that the PHP output (the response being processed) is formatted at XML code, and by converting into an actual XML object, that is what lets you used the data given with XML functionality.

Like I said, I don't mess with XML much at all, so someone else may be able to elaborate more. Myself, for scripts designed to work with AJAX, I set the output to be JSON