JSON.parse:意外的数据结束错误
对于下面的代码,我收到此错误:
JSON.parse:意外的数据结束
at line var data = 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()); }
这里错误的原因是什么?
指定"json"
,已经解析了回调的数据参数。 这里不需要调用JSON.parse
。