0

In my php file I have the following code

//create SQL to select schedule
 $sql = "SELECT * FROM `Schedule`";

 //create result set
 $result = mysql_query($sql);

 $result_array[] = "";
 while($row = mysql_fetch_assoc($result))
 {
    $result_array[$row['ID']] = $row;
 }

 echo json_encode($result_array);

In my HTML file I have the following code

xmlhttp.onreadystatechange=function()
  {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
    var schedArray = JSON.parse(xmlhttp.ResponseText);

    alert(schedArray[0]["ID"]);
   }
  }
  xmlhttp.open("GET","getSchedule.php?q="+year,true);
  xmlhttp.send();

When I run my HTML code I get this error: Uncaught SyntaxError: Unexpected token u

I belive my data is encoded correctly because running

document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

in my ready state check nets me an array with the correct data:

{"0":"","1":{"ID":"1","Team":"Louisiana-Monroe","playDate":"2011-09-03","Conference":"Sun Belt","Rank":null,"Bowl":"0","Site":"H"},"3":{"ID":"3","Team":"Oklahoma","playDate":"2011-09-17","Conference":"Big 10","Rank":"1","Bowl":"0","Site":"H"},"4":{"ID":"4","Team":"Clemson","playDate":"2011-09-24","Conference":"ACC","Rank":"21","Bowl":"0","Site":"A"},"5":{"ID":"5","Team":"Wake Forest","playDate":"2011-10-08","Conference":"ACC","Rank":null,"Bowl":"0","Site":"A"},"6":{"ID":"6","Team":"Duke","playDate":"2011-10-15","Conference":"ACC","Rank":null,"Bowl":"0","Site":"A"},"7":{"ID":"7","Team":"Maryland","playDate":"2011-10-22","Conference":"ACC","Rank":null,"Bowl":"0","Site":"H"},"8":{"ID":"8","Team":"North Carolina State","playDate":"2011-10-29","Conference":"ACC","Rank":null,"Bowl":"0","Site":"H"},"9":{"ID":"9","Team":"Boston College","playDate":"2011-11-03","Conference":"ACC","Rank":null,"Bowl":"0","Site":"A"},"10":{"ID":"10","Team":"Miami (FL)","playDate":"2011-11-12","Conference":"ACC","Rank":null,"Bowl":"0","Site":"H"},"11":{"ID":"11","Team":"Virginia","playDate":"2011-11-19","Conference":"ACC","Rank":null,"Bowl":"0","Site":"H"},"12":{"ID":"12","Team":"Florida","playDate":"2011-11-26","Conference":"SEC","Rank":null,"Bowl":"0","Site":"A"},"13":{"ID":"13","Team":"Notre Dame","playDate":"2011-12-29","Conference":"IND","Rank":null,"Bowl":"1","Site":"N"},"14":{"ID":"14","Team":"Murray State","playDate":"2012-09-01","Conference":"Ohio Valley","Rank":null,"Bowl":"0","Site":"H"},"15":{"ID":"15","Team":"Savannah State","playDate":"2012-09-08","Conference":"MEAC","Rank":null,"Bowl":"0","Site":"H"}}

I cannot figure out why I am receiving this error. I am not using JQuery and I have included json2.js in my JavaScript. Any help somebody could provide would be great.

7
  • 1
    Your abbreviation may be concealing the actual error in the JSON. Also are there really empty strings used as property names? Commented Sep 5, 2012 at 15:07
  • Can you post the JSON after the encode? Commented Sep 5, 2012 at 15:07
  • 1
    Thats no helping any , can you provide the FULL json you are trying to parse Commented Sep 5, 2012 at 15:10
  • might not help you directly but please: don't use mysql_* and refer to xmlhttp as this inside the onreadystatechange handler (if (this.readyState === 4 && this.status === 200)) Commented Sep 5, 2012 at 15:12
  • I've added the full JSON after the encode as represented by the echo json_encode($result_array); The property names are empty strings because I am running straight from the SQL query Commented Sep 5, 2012 at 15:13

2 Answers 2

2

Your problem is, I think, here:

var schedArray = JSON.parse(xmlhttp.ResponseText);

It should be responseText, lower-case "r". What's happening is that the string "undefined" is what the parser is actually trying to interpret, and a string starting with "u" cannot be valid JSON.

You got it right in the code that populates the debugging element to show the response.

Sign up to request clarification or add additional context in comments.

5 Comments

+1, nice spotting indeed, I only looked at the last responseText :)
That helped! I changed it to a lower case r and I stopped getting that error. Unfortunately I now get unexpected token <. When I comment out the JSON.parse line I do not get the error.
Well there's no < character in the JSON you posted, so that doesn't make a lot of sense.
A broswer debugger like Firebug or the Chrome developer tools can help a lot here, because they make it easy to look at the response actually being received from the server.
So apparently 000webhost.com adds analytics code to the php return. That code begins with <!-- I have disabled it and everything seems to work correctly. Here's the URL I found to disable that code if anybody else is looking: members.000webhost.com/analytics.php?action=login
0

The code that constructs the result is wrong. It should read:

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
   $result_array[$row['id']] = $row;
}

5 Comments

The reason I added Num in there is because I only return one result. I believe it keeps overwriting $result_array. My output for echo json_encode($result_array); with that code is {"0":"","":{"ID":"15","Team":"Savannah State","playDate":"2012-09-08","Conference":"MEAC","Rank":null,"Bowl":"0","Site":"H"}}
@evenflow58: In that case the only explanation is that the original code is misleading and $row does not have an id key. Are you sure it's id and not ID? What are the exact names of the table columns?
@Jon I think I found the problem, though I agree with you that there's something wrong with how the response object is being built.
You are correct. I change it to $result_array[$row['id']] = $row; and go this result: {"0":"","1":{"ID":"1","Team":"Louisiana-Monroe","playDate":"2011-09-03","Conference":"Sun Belt","Rank":null,"Bowl":"0","Site":"H"}... However, I'm still getting the original error (Uncaught SyntaxError: Unexpected token u)
@evenflow58: There are two separate errors in your code, both mine and Pointy's answers are correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.