JSON.parse:意外的数据结束错误 [英] JSON.parse: unexpected end of data error

查看:120
本文介绍了JSON.parse:意外的数据结束错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于下面的代码,我收到此错误:

For the code below i am getting this error :

JSON.parse:意外的数据结束

JSON.parse: unexpected end of data

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

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

这里出现错误的原因是什么?

What is reason for the error here?

推荐答案

当您指定json时,您的回调数据参数将被解析。这里不需要调用 JSON.parse

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

这篇关于JSON.parse:意外的数据结束错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆