weixin_33694620 2016-04-19 16:28 采纳率: 0%
浏览 46

Ajax POST请求,parseerror

I'm trying to make a web chat room, but when I send a post request to the server, with this code:

var line_count = 0;

$.ajax({
    type: 'POST',
    url: '../scripts/engine.php',
    data: {'method': 'getMsg', 'line_count': line_count},
    dataType: 'json',
    error: function(request, error) {
        alert('Error: '+error);
    },
    success: function(data) {
        $.each(data.messages, function(i, val) {
            $('.messages').append(val);
        });
        line_count = data.srv_count;
    }
});

And:

<?php
$method = $_POST['method'];

switch($method) {
    case 'postMsg':
        $sender = $_SESSION['username'];
        $message = $_POST['message'];
        $time = $_POST['time'];

        fwrite(fopen('chat.txt', 'a'), '<div class="time">[' . $time . ']</div><div class="nickname">' . $sender . '</div>' . $message . "
");
        break;       
    case 'getMsg':
        $log = array();

        if(file_exists('chat.txt')) {
            $usr_count = $_POST['line_count'];
            $srv_msg = explode("
", file('chat.txt'));
            $srv_count = count($srv_msg);

            $log['srv_count'] = $srv_count;

            if($usr_count < $srv_count) {
                $i = 0;

                while(list($key, $val) = each($srv_msg)) {
                    if ($i > $usr_count) {
                        $log['messages'][i] = $val;
                    }
                    $i = $i+1;
                }
            }
        } else {
            $log = false;
        }

        echo json_encode($log);
        break;
}
?>

Then I get this error message from ajax error function: "Error: parseerror". I checked the code many times, but I don't see where's the issue. Thanks in advance

Thanks everybody for the fast answers I checked returned value from the server, and I undertood it was a server-side problem. returnded value:

Warning: explode() expects parameter 2 to be string, array given in X:\wwwoot\scripts\engine.php on line 17
{"srv_count":0}


I just edited my code into:

                $usr_count = $_POST['line_count'];
                $srv_msg = explode("
", file_get_contents('chat.txt'));
                $srv_count = count($srv_msg);

                $log['srv_count'] = $srv_count;

                if($usr_count < $srv_count) {
                    $i = 0;

                    while(list($key, $val) = each($srv_msg)) {
                        if ($i > $usr_count) {
                            $log['messages'][$i] = $val;
                        }
                        $i = $i+1;
                    }
                }

And now It's working fine.

  • 写回答

3条回答 默认 最新

  • weixin_33739646 2016-04-19 16:38
    关注

    parseerror just means that you're not getting JSON back from the server. It could be PHP throwing an error and printing the error markup before the json.

    check the console to see what is actually being returned from the server.

    you may alos want to check is_writable() before attempting to write to a file. it could be a simple permissions issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗