ajax请求返回具有正确输出的意外输出

时间:2017-11-30 14:16:34

标签: php jquery mysql ajax

这是我从ajax请求到php pdo的输出:

[{"sys_id":"1","task":"qwe","task_date":"11\/30\/2017 8:49 PM","task_person":"qwe","task_status":"0"},{"sys_id":"2","task":"asd","task_date":"11\/30\/2017 9:54 PM","task_person":"asd","task_status":"0"}]null

如图所示,有一个超出null的值,我无法弄清楚它来自我的代码的位置是:



function selecttask(action) {
  $.ajax({
    type: 'POST',
    url: '../include/demo.php',
    dataType: "json",
    data: {
      action: action
    },
    success: function(data) {

    }
  }).done(function(data) {



  });
}
selecttask("selectall");




我的demo.php是:

<?php
include_once("crud.php");
//include_once("../config/config.php");
//$con = new connect_pdo();
$crud = new Crud();

$action =  $_POST['action'];
$data =  $_POST['data'];
switch (strtolower($action)):
    case("selectall"):
        $table = "list_tbl";
        $selectall = $crud->selectall($table);

        echo json_encode($selectall, JSON_UNESCAPED_UNICODE);
        break;
    case("add"):
        $table = "list_tbl";
        $insert = $crud->insert($table,$data);

        echo json_encode($insert, JSON_UNESCAPED_UNICODE);
        break;


endswitch;
?>

然后crud是:

<?php
include_once("../config/config.php");

class Crud extends connect_pdo {

    public $_con;

    function __construct() {
        parent::__construct();
        $this->_con = $this->dbh();
    }
    public function selectall($table_name) {

        $queryselectall = "Select * from {$table_name}";
        $selectall = $this->_con->prepare($queryselectall);
        if ($selectall->execute()) {
            if ($selectall->rowCount() > 0) {
                $result = $selectall->fetchAll(PDO::FETCH_ASSOC);
                echo json_encode($result, JSON_UNESCAPED_UNICODE);
            }else{
                echo array('error'=> TRUE, 'message'=> 'No result found.');
            }
        }
    }

    public function insert($table_name, $res) {

        parse_str($res, $data);

        $limit = count($data);
        $ctr = 0;
        $columns = "";
        $values = "";
        foreach ($data as $key => $value) {
            $ctr++;
            $columns.= "{$key}";
            $values .= ":{$key}";
            if ($ctr < $limit) {
                $columns.= ",";
                $values .= ",";
            }
        }
        $query = "INSERT INTO {$table_name} ({$columns})VALUES({$values})";
        try {
            $create = $this->_con->prepare($query);
            foreach ($data as $key => $value) {
                $keys = ":{$key}";
                $create->bindValue($keys, $value, PDO::PARAM_STR);
            }
            if ($create->execute()) {
                $lastinserted_id = $this->_con->lastInsertId();
                echo array('error' => FALSE, 'message' => 'Data added successfully.', 'lastinserted_id' => $lastinserted_id, 'query' => $query);
            } else {
                echo array('error' => TRUE, 'message' => 'Execution failed, please contact system support!');
            }
        } catch (Exception $ex) {
            echo array('error' => TRUE, 'message' => $ex);
        }
    }

}
?>
  

从上面的代码中我无法确定null的来源。   我是否遗漏了因为ajax请求而导致null出现的原因

1 个答案:

答案 0 :(得分:0)

对于Crud类来说,通过运行查询并将结果作为数组返回来处理数据库交互可能是一个更好的架构(如果发生异常,则抛出异常)。然后你的demo.php脚本可以从Crud类方法中获取数组并处理输出(json编码,响应输出)。