我知道这已经被问了一百万次,但它对我不起作用。我有一个 PHP 脚本(get_pathway_allowed.php),它在硬编码时输出以下 json 格式
{"pathway_allowed":"n","comment":"one comment"}{"pathway_allowed":"y","comment":"comment two"}{"pathway_allowed":"n","comment":"comment three"}{"pathway_allowed":"n","comment":"comment four"}
阿贾克斯脚本:
$('.pathway_button').click(function() {
alert(caseId + ' ' + currentLevel);
$.ajax({
type: "POST",
url: "scripts/get_pathway_allowed.php",
data: {case_id: caseId, level: currentLevel};
dataType: "json",
cache: false,
success: function(response) {
alert(response[0].pathway_allowed);
}
});
});
现在alert(caseId + ' ' + currentLevel);
显示两个变量的正确初始值。但是成功后我没有看到警报。如果我删除dataType: "json",
,我会看到未定义值的成功警报(预期......)。
get_pathway_allowed.php 有:
$case = $_POST['case_id'];
$level = $_POST['level'];
$query = "SELECT * FROM pathway WHERE level = '$level' AND case_fk = '$case'";
$result = mysql_query($query, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$caseData = array(
'pathway_allowed' => $row['pathway_allowed'],
'comment' => $row['comment']
);
print json_encode($caseData);
}
正如我之前所说,硬编码:
$case = '10';
$level = '1';
输出json数据如上OK。我究竟做错了什么?