首页 > 解决方案 > 为什么我的 PHP 在从 AJAX 调用时无法识别我的其他类?

问题描述

我创建了一个包含一些产品类别的数据库。我已经直接在 HTML 中测试了这些类并且它们可以工作。我可以通过实例化 PHP 类直接调用产品并填充我的网站商店。我想从 ajax 调用这个函数,这样我就可以在不重新加载页面的情况下增加产品的数量,但是当我调用 ajax 函数时,它说父类无法识别。所以我调用 productsview 类方法 PopulateStoreProducts 但它抛出并错误说它无法找到类产品,即使它已被扩展并且如果我调用该类它工作。不确定我是否必须使用 JSON 编码,因为它只需运行它即可工作。这是代码:

产品视图类(独立调用时有效)


    <?php

class ProductsView extends Products{
public function populateStoreProducts(){
        
        $storeproducts = $this->getAllProducts();
        
        $numberOfProducts = count($storeproducts);
        
        $id = array_column($storeproducts, 'ID');
        $name = array_column($storeproducts, 'Name');
        $description = array_column($storeproducts, 'Description');
        $price = array_column($storeproducts, 'Price');
        $image = array_column($storeproducts, 'Image_Link');
        // echo print_r($first_names[1]);
        
        $i = 1;
        $j = 0;
        
        $output = [];
        
        
        while($i<$numberOfProducts){        
            $i++;
            $j++;
            
            $output[$j] = 
             '<div class="col-lg-3 col-md-6 mb-4">'.
             '<div id="'.$id[$j]. 
             '" class="card h-100">'.
             '<img class="card-img-top" src="'.$image[$j].'" alt="">'.
             '<div class="card-body">
                    <h2 class="card-title">'.$name[$j].'</h2>'.
             '</hr>'.
             '<h4>Price '.$price[$j].'</h4>'.
             '</div></div></div>';


             echo $output[$j];
}
}

用于实例化类的 PHP 文件(独立调用时有效)

<?php

    include_once 'productsview.class.php';
    
            $testProduct = new ProductsView();
            $testProduct->populateStoreProducts();
        
        
?>

最后是js文件

$(document).ready(function(){

$("#products").load("classes/populatestore.php");

});

我的脚本链接在正文的末尾,我的 jquery 链接在头部。有谁知道我做错了什么?致命错误:未捕获的错误:未找到“产品”类。

标签: phphtmljqueryajax

解决方案


推荐阅读