蓝极冰焰 2024-09-24 22:32 采纳率: 42.3%
浏览 13
问题最晚将于10月02日00:00点结题

关于:接收到的数据不是有效的JSON格式

请大家帮我看下,下面这段代码,点击按钮发送的时候总是提示语法错误(接收到的数据不是有效的JSON格式),请问是什么情况


<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once '../connections/games.php';
session_start();
$scriptDir = __DIR__;
$judge = basename($scriptDir);
$station = $_SESSION['station'];
$game = isset($_GET['game']) ? $_GET['game'] : null;
$m_name = isset($_GET['name']) ? $_GET['name'] : null;
$date = isset($_POST['date']) ? $_POST['date'] : null;

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../../css/op.css">
<title>日程操作</title>
</head>
<body>
<br>
<?php
    // 检查是否提交了表单

        $statusesall = array("待开赛", "正在检录", "检录完成", "进行中", "已完赛"); // 要查询的状态列表
        $inStatusesall = "'" . implode("', '", $statusesall) . "'";

        if(!empty($date)) {      
            $sql = " SELECT * FROM m_dk_date 
                     WHERE `date` =:date AND game =:game AND name =:name AND $judge = :judge AND station = :station AND now IN ($inStatusesall)
                     ORDER BY `id` ASC
                   ";
                
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':date', $date);
            $stmt->bindParam(':game', $game);
            $stmt->bindParam(':name', $m_name);
            $stmt->bindParam(':judge', $username);
            $stmt->bindParam(':station', $station);
            $stmt->execute();
            $order = $stmt->fetchAll(PDO::FETCH_ASSOC);
        } else {
            $sql = " SELECT * FROM m_dk_date 
                     WHERE game =:game AND name =:name AND $judge = :judge AND station = :station AND now IN ($inStatusesall)
                     ORDER BY `id` ASC
                    ";
       
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':game', $game);
            $stmt->bindParam(':name', $m_name);
            $stmt->bindParam(':judge', $username);
            $stmt->bindParam(':station', $station);
            $stmt->execute();
            $order = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
       
        $statuses = array("待开赛", "正在检录", "检录完成", "进行中"); // 要查询的状态列表
        $inStatuses = "'" . implode("', '", $statuses) . "'";
        $sqlCount = " SELECT COUNT(*) AS total_now_count FROM m_dk_date
                      WHERE game =:game AND name =:name AND $judge = :judge AND station =:station AND now IN ($inStatuses) ";
    
        $stmtCount = $pdo->prepare($sqlCount);
        $stmtCount->bindParam(':game', $game);
        $stmtCount->bindParam(':name', $m_name);
        $stmtCount->bindParam(':judge', $username);
        $stmtCount->bindParam(':station', $station);
        $stmtCount->execute();
        $countResult = $stmtCount->fetch(PDO::FETCH_ASSOC);
        $totalnowCount = $countResult['total_now_count'];
    
    ?>
<div style="display:flex">
    <form>    
        <button id="pzgg" name="pzgg" style="background:#ffba00">赛程 -> 配置到公告</button>
    </form>
</div>
<a> 还有<?php echo $totalnowCount;?>项待完赛</a>
<div style="border-radius: 20px; background-color:#F8F8F8;">

    <table style="font-family: inherit;color:#555">
    <thead>
        <tr align="center" style="font-weight:bold;font-size:10px;">
         <th style="background:#3d77b3">顺序<br>Order</th>
         <th style="background:#3d77b3">状态<br>State</th>
         <th style="background:#3d77b3">单元<br>Unit</th>
         <th style="background:#3d77b3">时间<br>Time</th>
         <th style="background:#3d77b3">组别<br>Group</th>
         <th style="background:#3d77b3">赛程<br>Schedule</th>
         <th style="background:#3d77b3">主队<br>Team A</th>
         <th style="background:#3d77b3">对阵<br>VS</th>
         <th style="background:#3d77b3">客队<br>Team B</th>
         <th style="background:#3d77b3">备注<br>Remark</th>
         </tr>
    </thead>
 
    <tbody>
         <?php
        foreach ($order as $row):
        ?>
         <tr style="font-weight:bold;font-size:10px;">
         <td><?php echo htmlspecialchars($row['order']);?></td>
         <td><?php echo htmlspecialchars($row['now']); ?></td>
         <td><?php echo htmlspecialchars($row['apm']); ?></td>
         <td><?php 
                $time = substr(htmlspecialchars($row['time']),0,5);
                echo $time; ?></td>
         <td>
            <?php echo htmlspecialchars($row['group']); ?></td>
         <td><?php echo htmlspecialchars($row['schedule']); ?></td>
         <td style="display: flex;align-items: center;justify-content: right;"><?php 
                echo htmlspecialchars($row['team_a']); 
                $team_a = htmlspecialchars($row['team_a']);
                $team_flag_a_Path = '../../image/d_logos/'.$m_name."/".$team_a.'.png';
                if (file_exists($team_flag_a_Path)) {
                    echo '<img src="'.$team_flag_a_Path.'" height="32px">';
                }
                if (!empty($row['code_a'])){
                echo '<span style="width:54px;height:30px;background:#3d77b3;border-radius: 15px;color:#fff;font-size:22px">'.htmlspecialchars($row['code_a'])."</span>";
                }?>
        </td>

         <td style="align-items: center;">
            <?php echo '<span style="width:32px;height:32px;background:#d10000;border-radius: 16px;color:#fff;font-size:22px">vs</span>';?>
        </td>

        <td style="display: flex;align-items: center;justify-content: left;">
            <?php
                if (!empty($row['code_b'])){
                    echo '<span style="width:54px;height:30px;background:#ffba00;border-radius: 15px;color:#fff;font-size:22px">'.htmlspecialchars($row['code_b'])."</span>";
                }
                $team_b = htmlspecialchars($row['team_b']);
                $team_flag_b_Path = '../../image/d_logos/'.$m_name."/".$team_b.'.png';
                if (file_exists($team_flag_b_Path)) {
                    echo '<img src="'.$team_flag_b_Path.'" height="32px">';
                }
                echo htmlspecialchars($row['team_b']);
                ?></td>
         <td><?php echo htmlspecialchars($row['remark']); ?></td>
         </tr>
         <?php
        endforeach;
        ?>
    </tbody>

    </table>

    </form>
    
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
// 以下,将赛程的情况配置到结果中,用以公告查询
    $(document).ready(function() {
        $('#pzgg').click(function() {
            var orderData = [];
            var date = $('#date-select').val();

            $('tbody tr').each(function() {
                var rowData = {
                    date: date,
                    order: $(this).find('td:eq(0)').text(),
                    now: $(this).find('td:eq(1)').text(),
                    apm: $(this).find('td:eq(2)').text(),
                    group: $(this).find('td:eq(4)').text(),
                    schedule: $(this).find('td:eq(5)').text(),
                    code_a: $(this).find('td:eq(6) span').text(),
                    code_b: $(this).find('td:eq(8) span').text()
                };

                orderData.push(rowData);
            });
            var m_name = '<?php echo htmlspecialchars($m_name); ?>';
            var game = '<?php echo htmlspecialchars($game); ?>';

            $.ajax({
                url: 'dd_result_into.php', 
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    data: orderData,
                    date: date,
                    m_name: m_name,
                    game: game
                }),

                dataType: 'text',
                success: function(response) {
                    console.log('数据发送成功:', response);
                    // 处理响应
                },
                error: function(xhr, status, error) {
                    console.error('数据发送失败:', status, error, xhr.responseText);
                    // 根据需要处理错误
                }
            });
        });
    });
</script>

</html>


  • 写回答

22条回答 默认 最新

  • 阿里嘎多学长 2024-09-24 22:51
    关注

    AIGC生成:


    关于:接收到的数据不是有效的JSON格式

    你好!😊

    根据你的代码,问题可能出在 JSON 数据的解析上。 PHP 的 json_decode() 函数可以将 JSON 字符串转换为 PHP 对象或数组。然而,如果 JSON 字符串不正确,json_decode() 将返回 NULL,导致语法错误。

    请检查你发送的 JSON 数据是否正确,是否包含语法错误或无效字符。同时,你也可以使用 json_last_error() 函数来获取 JSON 解析错误的信息。

    如果你确定 JSON 数据正确,可能是服务器端的 JSON 解析配置问题。可以尝试使用 json_encode() 函数将 JSON 数据重新编码,或者检查服务器端的 PHP 配置是否正确。

    如果你需要更多帮助,请提供更多代码和详细信息。


    评论

报告相同问题?

问题事件

  • 创建了问题 9月24日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?