0

我有一个在所有托管环境中都无法正常工作的功能。返回的数据为 JSON 格式。我认为问题出在 eval() 上。我可以为 eval() 使用任何替代方法吗?请帮我调试这个或建议任何替代方法。

三天来,我一直在为这个错误挠头(它只出现在少数主机上,但在其他主机上运行良好)

Load.php 中的数据如下

while($row=mysqli_fetch_array($sql)){
        $text=$row['mesg'];
        $fro=$row['fro'];
        $tocomr=$row['tocom'];
        $last_msg_id_db=$row_m['maxid'];


        $response=array();

        //$response['msg']=$table;
        $response['msg']=$text;
        $response['last_msg_id_db']=$last_msg_id_db;
        $final_response[]=$response;

    }
          echo json_encode($final_response);

并且数据返回为

    [{"msg":"hi<br>","last_msg_id_db":"173"}]
    main.php:96

    main.php:96
   [{"msg":"heloo<br>","last_msg_id_db":"174"}]

jquery函数如下。

function chat_com_one(id, name) {
$('#chatcom').show('fast');
(function chatcom_load_one(id, name) {

    $.ajax({
        type:"Post",
        url:"load.php",
        data:{
            tocom:id,
            last_msg_id:last_msg_id
        },


        success:function(data) {

            var json = eval(data);
            $.each(json, function(i, row) {
                $("#commidwin").append(row['msg']);
                last_msg_id = row['last_msg_id_db'];
            });
            setTimeout(chatcom_load_one(id, name), 500);
        },
        error:function(XMLhttprequest, textstatus, errorthrown) {
            alert("error:" + textstatus + "(" + errorthrown + ")");
        }




    });
}(id, name));
}

Chrome的日志中给出了以下错误

Uncaught SyntaxError: Unexpected identifier main.php:95
 $.ajax.success main.php:95
fire jquery.min.js:974
self.fireWith jquery.min.js:1082
done jquery.min.js:7788
callback
4

1 回答 1

0

请不要使用评估。只需设置您的dataTypeas json,jQuery 就会为您解码 JSON。

$.ajax({
    type:"POST",
    url:"load.php",
    dataType:"json",
    data:{
        tocom:id,
        last_msg_id:last_msg_id
    },
    success:function(data) {
        var json = data;
        $.each(json, function(i, row) {
            $("#commidwin").append(row['msg']);
            last_msg_id = row['last_msg_id_db'];
        });
        setTimeout(chatcom_load_one(id, name), 500);
    },
    error:function(XMLhttprequest, textstatus, errorthrown) {
        alert("error:" + textstatus + "(" + errorthrown + ")");
    }
});
于 2012-12-03T11:53:37.370 回答