簡體   English   中英

PHP / AJAX解析錯誤

[英]PHP/AJAX parse error

我正在嘗試使用我的PHP shoutbox和使用AJAX查詢,以便頁面不必重新加載以發布呼喊。 任何幫助將不勝感激!

我被困 - 我一直收到一個解析錯誤:

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

我有shoutBox.php,它包含HTML和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 + "\nError:" + 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>

和postData.php,它(應該)執行查詢。

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

?>  

刪除一行

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

?>  

您的解析錯誤來自此行:

$( “#ADATA”)文本(數據)。

您不能將Json編碼數據發送到DOM中的id。 你想看到數據的價值,試試這個:

警報(JSON.stringify(數據));

其他的東西,在你的PHP,避免退出,忘了if和else。

正確的方法是嘗試,抓住。 你嘗試,如果發生錯誤,捕獲異常。

在函數外部放置一個全局變量並將awswer放入其中。

之后:

echo json_encode($ aswer); $ connect = null; //總是關閉你的連接。

我希望它會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM