查询结果导致JQuery函数中的错误响应

时间:2018-08-19 14:01:55

标签: javascript php jquery sql json

我有一个问题,单击表的,我调用了javascript函数,而javascript函数又调用了php中的函数以获取数据库中的数据。单击表行有效,SQL起作用,并且从console.log命令中我知道reponseText中有答案。但它无法正常工作,并且我又收到了错误,我将发布代码。我希望你能帮助我。

文件config.php

//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','toor');

try{
    //create PDO connection
    $db = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
    //show error
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}

//include the user card, pass in the database connection
include($_SERVER['DOCUMENT_ROOT'].'cards.php');
$card = new card($db);

文件cards.php

<?php

class card
{

    private $_db;

    function __construct($db){
        $this->_db = $db;
    }

    public function view_card_id($id)
    {
        $rows = array();

        $statement = $this->_db->prepare('SELECT * FROM card_details WHERE card_id = :card_id');
        $statement->execute(array(':card_id' => $id));

        $numrows = $statement->fetch(PDO::FETCH_ASSOC);

        if($numrows < 1) {
            $this->error = "Error";
            return false;
        } else {
            $statement->bindColumn("card_id", $cid);
            $statement->bindColumn("a", $a);
            $statement->bindColumn("b", $b);

            $rows[] = array('card_id' => $numrows['card_id'], 'a' => $numrows['a'], 'b' => $numrows['b']);

            while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { 
                $row = array('card_id' => $cid, 'a' => $a, 'b' => $b);
                $rows[] = $row;
            }

            return $rows;
        }

    }

文件index.php

<?php

//include config
require_once($_SERVER['DOCUMENT_ROOT'].'config.php');

?>

html code ....

<script src="/js/Cards.js" type="text/javascript"></script>

文件Cards.js

$('#table-cards tr').click(function() {
var id = $(this).find("a").text();

$.ajax({
        type: 'POST',
        url: '/classes/cardsFunc.php',
        dataType:'text',
        data: {functionname: "view_card", id: id },
        success: function(response){
            //Use response
            alert("Server echo: "+response);
            console.log(response);
        },
        error: function(msg){
            console.log(msg);
            alert("Error: "+msg);
        }
    });

 });

在Cards.js文件中,一旦调用$ .ajax函数,它不会返回成功而是返回错误,但是在console.log中,我在responseText条目下看到了已执行查询的数组。

也就是说,在错误响应中,我看到了查询结果,理论上应该是对成功的响应。

我也尝试使用

$.post('/classes/cardsFunc.php', { functionname: 'view_card', id: id }, function(data){

});

但没有

文件cardsFunc.php

<?php

//include config
require_once($_SERVER['DOCUMENT_ROOT'].'config.php');

if(isset($_POST['functionname']) && $_POST['functionname'] == "view_card"){

    $card_view = $card->view_card_id($_POST['id']);
    print json_encode($card_view);

}


?>

感谢您为我奉献的时间

我注意到,如果我在cardsFunc.php文件中重新创建与数据库的连接,则一切正常,但是我不明白为什么,因为一切都在config.php文件中。 像这样:

文件cardsFunc.php

<?php

//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','toor');

try{
    //create PDO connection
    $db = new PDO("mysql:host=".DBHOST.";charset=utf8mb4;dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
    //show error
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}

//include the user card, pass in the database connection
include($_SERVER['DOCUMENT_ROOT'].'cards.php');
$card = new card($db);

if(isset($_POST['functionname']) && $_POST['functionname'] == "view_card"){

    $card_view = $card->view_card_id($_POST['id']);
    print json_encode($card_view);

}


?>

0 个答案:

没有答案