繁体   English   中英

PHP:AJAX请求上出现500 Internal Server Error

[英]PHP: 500 Internal Server Error on AJAX request

我有一个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?

您的连接中不应使用引号。

$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

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

<?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>';

顺便看一下si写的echo样式,这称为echo多个参数,这比与串联更好.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM