weixin_33693070 2017-01-22 02:58 采纳率: 0%
浏览 10

PHP / AJAX解析错误

I'm trying to take my PHP shoutbox and query using AJAX so that the page doesn't have to reload for shouts to be posted. Any help would be greatly appreciated!

I am stuck - I keep receiving a parse error:

18:47:27.216 Details: parsererror
Error:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 1 index.php:100:4

I've got shoutBox.php, which holds the HTML and AJAX:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head> 

<script type="text/javascript">
function sendChatMessage(msg)
{
    $.ajax({
        type: "POST",
        url: "login/postData.php",
        data: {
            thisPost: msg   
        },
        dataType: "json",
        success: function(data)
        {
            if (data.error === false)
            {
                $("#aData").text(data);
                console.log("Post submitted.");
                ajax.reload();

            }
            else
            {
                console.log("Post was not submitted.");
            }
        },
        error: function(xhr, desc, err)
        {
            console.log(xhr);
            console.log("Details: " + desc + "
Error:" + err);
        }
    });
}
</script>

<div class="shoutbox">

    <h1 class="panel-heading"><strong>Federal</strong> Communications Array</h1>
    <hr>
    <ul class="shoutbox-content"></ul>

    <div class="panel">
    <form method="post">
    <p>
    <table border="0" align="center" style="height: auto;" width="90%">
        <tbody>
            <tr>
                <td><a></a></td>
                <td>

                <div class="input-group">

                    <span class="input-group-addon">Say:</span>
                    <input class="form-control" id="aData" name="post" maxlength='255' placeholder="Enter a message..."></input>

                    <div class="input-group-btn">
                        <button class="btn btn-success" onClick="sendChatMessage(document.getElementById('aData').value); return false;">Send</button>
                    </div>

                </div>


                </td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <p>
    </form>
    </div>

    <div class="panel">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th width="100px">Timestamp</th>
                    <th width="120px">Username</th>
                    <th width="980px">Message</th>
                    <th></th>
                </tr>
            </thead>


        </table>
    </div>



</div>

And postData.php, which (is supposed to) execute the query.

<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

echo('Script begin.');

require "dbconf.php"; // db details

$connect = mysql_connect($host,$username,$password);

mysql_select_db($db_name,$connect);

$post = '';

    if(empty($_POST['aData'])) 
    {
        exit(json_encode([
        'error' => 'You must enter a message!',
        ]));

    } else {

        $post = trim($_POST['aData']);

        $playerUsername = $_SESSION['username'];

        $idQuery = "SELECT `id` FROM `members` WHERE `username`='$playerUsername'";

        $playeridQuery = mysql_query($idQuery);

        $playerID = mysql_result($playeridQuery, 0);

        mysql_query("INSERT INTO `shoutboxPosts` SET `id`='$playerID', `username`='$playerUsername', `post`='$post'");

    }



    exit(json_encode([
        'error' => false,
    ]));

?>  
  • 写回答

2条回答 默认 最新

  • 妄徒之命 2017-01-22 03:04
    关注

    Remove a single line

    <?php
    
    ini_set('display_errors', 'On');
    error_reporting(E_ALL | E_STRICT);
    
    //echo('Script begin.'); Remove this line. It's violating JSON format
    
    require "dbconf.php"; // db details
    
    $connect = mysql_connect($host,$username,$password);
    
    mysql_select_db($db_name,$connect);
    
    $post = '';
    
        if(empty($_POST['aData'])) 
        {
            exit(json_encode([
            'error' => 'You must enter a message!',
            ]));
    
        } else {
    
            $post = trim($_POST['aData']);
    
            $playerUsername = $_SESSION['username'];
    
            $idQuery = "SELECT `id` FROM `members` WHERE `username`='$playerUsername'";
    
            $playeridQuery = mysql_query($idQuery);
    
            $playerID = mysql_result($playeridQuery, 0);
    
            mysql_query("INSERT INTO `shoutboxPosts` SET `id`='$playerID', `username`='$playerUsername', `post`='$post'");
    
        }
    
    
    
        exit(json_encode([
            'error' => false,
        ]));
    
    ?>  
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私