So... I am trying to use JavaScript to parse an object returned from an PHP file. The relevant code looks like this:
var name = document.getElementBId("name").value;
var XHR = new XMLHttpRequest();
XHR.open("GET', 'lookup.php?name=' + name, true);
XHR.onreadystatechange = function (){
try{
alert("Attempting to parse");
if ((XHR.readyState === 4) && (XHR.status === 200)) {
alert("Parsing");
var jsonresponse = JSON.parse(XHR.responseText);
alert("Is this being skipped?");
....
Here's the php being returned:
{
"name": ABC Elementary,
"addr": 3000 County Road 29 Alberta AL 36720-2817,
"county": Wilcox, "district": 6;
}
This program is supposed to submit a school's name for a math tournament and use the rest of that info to display the school's address, county, and district once its name has been selected. I picked ABC Elementary as the debugger because it's easy to type.
Unfortunately, the script won't proceed past this point. I'm getting an alert from the alert("Parsing")
, but not from alert("Is this being skipped?")
. I'm also not getting anything past that point within the XML.onreadystatechange()
, although I am still getting alerts from outside the block.
My guess is that it has something to do with how JSON.parse()
handles data, or at least it has to do with that line.
Also, I know that other people have asked about this, and I have been looking for these answers, but nobody seems interested in knowing what exactly JSON.parse()
is doing to that data. If anyone could enlighten me, I would be very grateful.