JSON.parse: unexpected end of data error

For the code below, I get this error:

JSON.parse: unexpected end of data

in the line var data = JSON.parse (json);

The following code is used:

$(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" ); }); }); 

back end 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()); } 

What is the cause of the error here?

+4
source share
1 answer

When you specify "json" , the data argument for your callback will already be parsed. There is no need to call JSON.parse .

+9
source

All Articles