首页 > 解决方案 > axios get方法vue可以获取json数据但不能获取其属性

问题描述

我想在 vue 应用程序中从我的数据库中获取数据。正在使用 axios 发出请求。我将数据作为一个整体对象获取,但是一旦我尝试达到某个属性,应用程序就会响应它是未定义的。我的猜测是,数据并没有真正转换为 JSON 格式,因此无法访问属性。我已经尝试过 JSON.parse() 但它不起作用。

我控制台记录了数据访问 第一个控制台输出是整个对象,第二个输出应该是一个特定的属性

Index.html(进行 axios 调用的地方)

    methods: {
    getAllBooks(){
        axios.get("url").then(
            function(response) {
                if(response.data.error) {
                    app.errorMsg = JSON.parse(response.data.message);
                } else{
                    app.buecher = response.data;
                    console.log(typeof app.buecher);
                    console.log(JSON.stringify(app.buecher)); //if I remove stringify here it logs [object object]
                    console.log(JSON.stringify(app.buecher.ProduktID));

进程.php

$response = array();

while ($zeile = mysqli_fetch_assoc($db_erg))   
{
    $response[] = $zeile;

}

echo json_encode($response); // as far as I understood $response is JSON from that moment on
mysqli_free_result( $db_erg );
exit;
}

?>

有人看到这里的问题吗?

标签: phpjsonvue.jsaxios

解决方案


推荐阅读