Nicht erfasster Syntaxfehler beim Parsen von JSON auf eine Antwort mit einer AJAX-Anfrage: Unerwartetes Token „<', „<br />
P粉846294303
P粉846294303 2023-11-01 10:06:57
0
1
709

Ich verwende datefrom und dateto, um ein Dropdown-Menü zu erstellen und dann die Daten zwischen diesen Daten anzuzeigen, aber es wird nicht angezeigt und ich erhalte ständig diese Fehlermeldung in der Konsole.

VM652:1 
        
       Uncaught SyntaxError: Unexpected token '<', "<br />
<b>"... is not valid JSON
    at JSON.parse (<anonymous>)
    at Object.success (<anonymous>:18:30)
    at c (jquery-3.6.0.min.js:2:28327)
    at Object.fireWith [as resolveWith] (jquery-3.6.0.min.js:2:29072)
    at l (jquery-3.6.0.min.js:2:79901)
    at XMLHttpRequest.<anonymous> (jquery-3.6.0.min.js:2:82355)

Das ist das Drehbuch:

<script>
  // Add an event liste ner for the form submission
  document.querySelector('#submit-button').addEventLi stener('click', function()  {
    // Prevent the default form submission behavior
    event.preventDefault();

    // Get the selected values of the "fromdate" and "todate" dropdown menus
    var fromDate = $('#fromdate').val();
    var toDate = $('#todate').val();

    // Send the AJAX request to the PHP script
    $.ajax({
      type: "POST",
      url: "functions/search-sales_function.php",
      data: { fromDate: fromDate, toDate: toDate },
      success: function (response) {
        // Parse the JSON response
        var salesData = JSON.parse(response);

        // Loop over the sales data and insert it into the table
        for (var i = 0; i < salesData.length; i++) {
          var date = salesData[i]['date'];
          var sales = salesData[i]['total_sales'];
          $('#sales-table tbody').append(`
            <tr>
              <td>${date}</td>
              <td>${sales}</td>
            </tr>`
            );
        }
      },
    });
  });

</script>

Dies ist die PHP-Datei:

<?php
    include "db_conn.php";
      // Get the values of the "fromdate" and "todate" dropdown menus
      $fromDate = $_POST['fromdate'];
      $toDate = $_POST['todate'];

      // Check the connection
      if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
      }

      // Write a SELECT statement to get the sales data between the selected dates
      $sql = "SELECT date, SUM(sales) as total_sales FROM sales WHERE date BETWEEN '$fromDate' AND '$toDate' GROUP BY date ORDER BY date ASC";

      // Execute the SELECT statement and fetch the results
      $result = mysqli_query($conn, $sql);

      // Loop over the results and add a row to the table for each date
      while ($row = mysqli_fetch_assoc($result)) {
        $date = $row['date'];
        $sales = $row['total_sales'];
        echo "<tr>
                <td>$date</td>
                <td>$sales</td>
              </tr>";
      }

      // Close the connection
      mysqli_close($conn);
    ?>

Ich möchte Zeilen mit Datumsangaben zwischen ausgewählten Daten anzeigen, aber der Fehler wird weiterhin im Konsolenprotokoll angezeigt und die Tabelle wird nicht angezeigt

P粉846294303
P粉846294303

Antworte allen(1)
P粉318928159

该错误与您的ajax方法的响应错误有关。该代码发送了 ajax 请求的结果。您应该在 php 文件中使用 echo json_encode($data);

$data = [];
 while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
        echo json_encode($data);
 }
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!