JavaScript and client side scripting.
Moderator: General Moderators
gautamz07
Forum Contributor
Posts: 331 Joined: Wed May 14, 2014 12:18 pm
Post
by gautamz07 » Sun Sep 28, 2014 8:44 am
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;
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Sun Sep 28, 2014 11:13 am
It's saving a property of the xhr object to a local variable.
twinedev
Forum Regular
Posts: 984 Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio
Post
by twinedev » Sun Sep 28, 2014 6:49 pm
More specifically, it is taking the response from the AJAX request and converting it to XML format and then assigning that to parsedResponse
gautamz07
Forum Contributor
Posts: 331 Joined: Wed May 14, 2014 12:18 pm
Post
by gautamz07 » Mon Sep 29, 2014 2:04 pm
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 ????
gautamz07
Forum Contributor
Posts: 331 Joined: Wed May 14, 2014 12:18 pm
Post
by gautamz07 » Tue Sep 30, 2014 6:02 am
Or is it converting The PHP response to XML or the JS to XML .. I am confused
twinedev
Forum Regular
Posts: 984 Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio
Post
by twinedev » Wed Oct 01, 2014 1:49 am
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