【问题标题】:PHP: 500 Internal Server Error on AJAX requestPHP:AJAX 请求出现 500 内部服务器错误
【发布时间】:2017-11-25 09:53:49
【问题描述】:

我有一个 bdd MySQL,它会定期更新人工智能生成的文本。我想将这个无限文本显示为网络发布,所以我们这样做了,作为一个粗略的测试:

<head>
    <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div id="myTable"></div>

    <script type="text/javascript">  
        function page_refresh() {
            $.ajax({
                url: 'getData.php',
                type: 'GET',
                success: function(data) { 
                    $('#myTable').html(data);
                },
                error: function(request,error) {
                    alert("Request error: " + JSON.stringify(request));
                }
            });
        }

        var period = 10000; // NOTE: period is passed in milliseconds
        setInterval(page_refresh, period); 
    </script>
</body>

谁在打电话getData.php

<?php

$dbhandle = new PDO("mysql:host='localhost';dbname='writing'", "root", "*********");  //this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //this is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql); 

if ($query->execute() == FALSE) {
    die("Query error: " . implode($query->errorInfo(), ' '));
} //this is a measure to close the connection if there is an error

echo('<table>');
echo('<tr><th>id</th><th>date</th><th>text</th></tr>');

while ($row = $query->fetch()) {
    echo('<tr>');
    echo('<td>').$row ['id'].'</td>');
    echo('<td>').$row ['date'].'</td>');
    echo('<td>').$row ['text'].'</td>');
    echo('</tr>');
}

echo('</table>');

我们遇到了两个问题。首先,我们只得到一个空白页。其次,在一两秒后,我们得到了这个错误信息:

{Request error : "readyState":4,"responseText":"","status":500,"statusText":"Internal Server Error"}

有什么想法吗?

我的错误日志

> [Thu Jun 22 06:25:01.645901 2017] [mpm_prefork:notice] [pid 17562]
> AH00163: Apache/2.4.18 (Ubuntu) configured -- resuming normal
> operations [Thu Jun 22 06:25:01.645918 2017] [core:notice] [pid 17562]
> AH00094: Command line: '/usr/sbin/apache2' [Thu Jun 22 08:12:32.453382
> 2017] [:error] [pid 19559] [client 77.198.111.62:57256] PHP Parse
> error:  syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 08:21:09.579406 2017]
> [:error] [pid 19560] [client 77.198.111.62:57430] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 08:21:09.582093 2017]
> [:error] [pid 19648] [client 77.198.111.62:57431] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:09:04.947344 2017]
> [:error] [pid 19559] [client 77.198.111.62:59193] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:09:11.454622 2017]
> [:error] [pid 19648] [client 77.198.111.62:59196] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:10:28.529637 2017]
> [:error] [pid 19561] [client 77.198.111.62:59227] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:10:32.709239 2017]
> [:error] [pid 19558] [client 77.198.111.62:59229] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 14 [Thu Jun 22 09:11:39.122539 2017]
> [:error] [pid 19559] [client 77.198.111.62:59270] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:11:51.476161 2017]
> [:error] [pid 19648] [client 77.198.111.62:59285] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php [Thu Jun 22 09:14:14.697094 2017]
> [:error] [pid 19557] [client 77.198.111.62:59440] PHP Parse error: 
> syntax error, unexpected ')', expecting ',' or ';' in
> /var/www/html/getData.php on line 13, referer:
> http://82.223.18.239/testTom.php

听从你的建议,我现在:

<!DOCTYPE html>
<head>

<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>

<body>
    <div id="myTable"></div>

    <script type="text/javascript">  
        function page_refresh() {
            $.ajax({
                url : 'getData.php',
                type : 'GET',
                success : function(data) { 
                    $('#myTable').html(data);
                },
                error : function(request,error)
                {
                    alert("Request error : "+JSON.stringify(request));
                }
            });
        }

        var period = 10000; //NOTE: period is passed in milliseconds
        setInterval(page_refresh, period); 
    </script>

</body>

至于 getData.php :

<?php
$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");//this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //thise is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql); 

if ($query->execute() == FALSE)
    { die("Query error: " . implode($query->errorInfo(), ' ')); } //this is a measure to close the connection if there is an error

    echo('<table>');
        echo('<tr><th>le nom de la rose</th></tr>');
        while ($row = $query->fetch()) {
            echo('<tr>');
                ##echo('<td>'.$row ['id'].'</td>');
                ##echo('<td>'.$row ['date'].'</td>');
                echo('<td>'.$row ['text'].'</td>');
            echo('</tr>');
        }
    echo('</table>');
?> 

它正在工作!使用此代码,页面每 X 秒刷新一次,但似乎一点也不流畅。如果我将它设置为每 100 毫秒刷新一次,它会像 F5 一样工作和卡顿。你知道如何解决吗?如何设置平滑刷新?有没有更有趣的方式在这里使用 ajax?

【问题讨论】:

  • 检查错误日志-getData.php可能有错误
  • implode($query-&gt;errorInfo(), ' ') 是错误的。它的第一个参数应该是胶水和第二个数组
  • 来自 PHP 手册 - Note: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments.
  • 检查你的 http 服务器的错误日志,如果是 Apache 他们通常住在/var/log/apache2/error.log
  • 请不要从您的问题中删除代码并替换为问题已解决。而是考虑将其中一个答案标记为已接受或发布包含修复的自我答案。

标签: javascript php mysql ajax pdo


【解决方案1】:

您的连接不应在...中包含引号

$dbhandle = new PDO("mysql:host='localhost';dbname='writing'", "root", "*********");  //this connects to your mysql table

应该是……

$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");  //this connects to your mysql table

【讨论】:

  • 谢谢!我改变了这个,我得到了 apache 错误日志。我仍然得到一个空白页,但我们可能已经修复了一个错误:)
  • 您的台词...echo('&lt;td&gt;').$row ['id'].'&lt;/td&gt;'); 您需要删除第一个关闭 ')`。这是几行,所以检查一下。
  • 喜欢吗? echo(''$row ['id'].'');对不起,我在这里缺乏技能^^
  • 它需要类似于echo('&lt;td&gt;'.$row ['id'].'&lt;/td&gt;');
【解决方案2】:

您的代码有一些错误,请看下面的代码:

<?php

$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");  //this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //this is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql); 

if ($query->execute() == FALSE) {
    die("Query error: " . implode(' ', $query->errorInfo()));
} //this is a measure to close the connection if there is an error

echo '<table>';
echo '<tr><th>id</th><th>date</th><th>text</th></tr>';

while ($row = $query->fetch()) {
    echo '<tr>';
    echo '<td>', $row ['id'], '</td>';
    echo '<td>', $row ['date'], '</td>';
    echo '<td>', $row ['text'], '</td>';
    echo '</tr>';
}

echo '</table>';

顺便看看我写的echo s的风格,这叫做echo multiple parameters,比和.串联更好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多