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,
        ]));
    
    ?>  
    
    评论

报告相同问题?