我有以下JS代码试图从PHP脚本中检索JSON对象,如下所示:
var url="http://lewspage.hostei.com/xword_php/getXword.php";
jQuery.getJSON(url, {name:title}, function(data) {
// ... handle response as above
console.log("Data is "+data);
});
但是,当我使用title =“P2”在google chrome中执行代码并检查控制台时,它不会记录任何内容或打印出任何错误消息,这意味着getJSON失败。当我尝试运行链接{{ 3}}手动,我收到回复:
{"currentPuzzle":[{"name":"P2","tiles":"327M,328U,329T,330H,331E,332F,333U,334C,335K,336A"}]}
这是一个有效的JSON对象。
我的PHP脚本如下:
<?php
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Obtain username
$name = $_GET["name"];
$sql = "SELECT * FROM CrossWordPuzzles where PuzzleName = '$name'";
$result = mysql_fetch_row(mysql_query($sql));
$response["currentPuzzle"]=array();
$puzzle=array();
$puzzle["name"]=$result[1];
$puzzle["tiles"]=$result[2];
array_push($response["currentPuzzle"], $puzzle);
// echoing JSON response
echo json_encode($response);
?>
我做错了什么?