Axios.get返回错误

时间:2018-02-09 20:04:24

标签: php mysql database get axios

我想从数据库中获取数据并将其写入vuejs表但我收到错误。它无法将数据解析为Json。 在我的数据库中,我有主列id,文本列username,firstname,lastname和date column birthday。

获取所有用户的功能:

getAllUsers: function () {
                        axios.get("api.php?method=index")
                            .then(function (response) {
                                console.log(response);
                                app.users = response.data.userlist;
                            });
                    },

api.php:用于控制数据库和网站之间的流量

<?php
 class API
{
protected $MYSQLi;


function __construct() 
{
    if (isset($_SERVER["DOCUMENT_ROOT"]) && $_SERVER["DOCUMENT_ROOT"] == "D:/xampp/htdocs")
    {       
        $this->MYSQLi = new mysqli("localhost", "root", "","test")or die("cannot connect"); 
    }
    else
    { 
        $this->MYSQLi = new mysqli("localhost", "", "", ""); //here i cleared the 
//database credentials
    }   
    date_default_timezone_set('Europe/Vienna');
}

public function doit()
{
    $get = "index";
    if (isset($_GET["method"])) $get = $_GET["method"];
    switch($get)
    {
        case "insert":
                            $this->insert();
                            break;          
        case "update":
                            $this->update();
                            break;
        case "delete":
                            $this->delete();
                            break;
        default: 
                            $this->getall();
    }
}



private function getall()
{
    $sql = "select * from user order by id";
    $lnk = $this->doSQL($sql);
    $userlist = Array();
    while($arr = $lnk->fetch_array(MYSQLI_ASSOC))
    {
        $userlist[] = $arr;
    }
    $this->outputJSON($userlist);
}

private function outputJSON($string)
{
    header('Content-Type: application/json');
    $output = @json_encode($string);
    if (!$output) 
    {
        $output = json_encode(Array("error"=> "Not parseable!"));
    }
    echo $output;
}

private function doSQL($sql,$debug= false,$doescape = false)
{
    $ret = FALSE;
    if ($debug) echo "SQL: " . $sql . "\n<br>\n";
    if ($doescape) $sql = $this->MYSQLi->real_escape_string($sql);
    try
    {
        $ret = $this->MYSQLi->query($sql);
        if (!$ret)
        {
            $arr = debug_backtrace();
            $ln = "";
            if (isset($arr[0]) && isset($arr[0]['line']))
            {
                $ln = " line " . $arr[0]['line'];
            }
            printf("<strong>SQL Statement: %s\n<br>SQL-Error" . $ln.": %s</strong>\n<br>",$sql, $this->MYSQLi->error);
        }
    }
    catch(Exception $e)
    {
        print_r($e);
    }
    return $ret;
}       

private function esc($STR)
{
    return $this->MYSQLi->real_escape_string($STR);
}

}

$obj = new API();
$obj->doit();

0 个答案:

没有答案