4
意外結束對於代碼下面我收到此錯誤:JSON.parse:數據錯誤
JSON.parse:在line VAR數據= JSON.parse(JSON)數據
意外結束;
使用的代碼是:
$(document).ready(function(){
$("#button1").click(function(){
$.post(
'script_1.php',
{ id: $('input[name="id"]', '#myForm').val() },
function(json) {
var data = JSON.parse(json);
if (data.length === 0){
alert('no data');
}
else{
$("input[name='title']").val(json.title);
$("input[name='age']").val(json.age);
}},
"json"
);
});
});
後端PHP是
$sql ="SELECT * FROM parent WHERE id = '$name'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if ($row) {
$row= array('title' => $row['title'],'rno' => $row['reportno'],'url' =>
$row['calc_url'], 'institution' => $row['institution']);
echo json_encode($row);
} else {
echo json_encode(array());
}
是什麼原因在這裏的錯誤?
DB的結果是什麼? –
注意:請注意[SQL注入](http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php)並使用['mysql_real_escape_string'](http://php.net/manual/) en/function.mysql-real-escape-string.php)放入'$ name',然後插入到你的'$ sql'字符串中。 – betamax
@betamax:我做到了.. –