4

For the code below i am getting this error :

JSON.parse: unexpected end of data

at line var data = JSON.parse(json);

the code used is:

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

the back end php is

$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 reason for the error here?

2
  • What is the result from the DB? Commented Aug 18, 2011 at 16:44
  • 1
    Note: Beware of SQL injection and use mysql_real_escape_string on $name before inserting into your $sql string. Commented Aug 18, 2011 at 16:47

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.