0

私のphpファイルには、次のコードがあります

//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);

私のHTMLファイルには、次のコードがあります

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();

HTML コードを実行すると、次のエラーが表示されます: Uncaught SyntaxError: Unexpected token u

実行しているため、データが正しくエンコードされていると思います

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

私の準備ができた状態で、チェックは正しいデータを持つ配列をネットに入れます:

{"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"}}

このエラーが発生する理由がわかりません。私は JQuery を使用しておらず、JavaScript に json2.js を含めています。誰かが提供できるどんな助けも素晴らしいでしょう。

4

2 に答える 2

2

あなたの問題は、ここにあると思います:

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

responseText、小文字の「r」である必要があります。何が起こっているかというと、文字列"undefined"はパーサーが実際に解釈しようとしているものであり、「u」で始まる文字列は有効な JSON ではありません。

デバッグ要素を設定して応答を表示するコードで正しく理解できました。

于 2012-09-05T15:24:16.383 に答える
0

結果を構築するコードが間違っています。それは読むべきです:

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
   $result_array[$row['id']] = $row;
}
于 2012-09-05T15:15:47.983 に答える