我最近在做一个小的练习项目,用tp5写了一个更新数据库的函数,然后将数据变成一个数组,最后return这个数组,然后又写了一个函数让这个函数调用上一个函数的返回值,但是最后在前台页面显示错误,错误提示500 (Internal Server Error);求大神帮主   public function  update_count ()
    {
        $value = $_POST['value'];
        $name = $_POST['name'];
        $total=0;
        $total_shopping = 0;
        $save_counts = 0;
        $data_base = Db::table('cart')->select();
        $shopping_list = Db::table('cart')->where('name',$name)->find();
        $promotion_goods = Db::table('promotion_goods')->where('name', $name)->find();
        $goods_name = Db::table('cart')->where('name',$name)->find();
        if ($shopping_list['count'] >= 0) {
                $new_counts = $shopping_list['count'] + $value;
            if ( $new_counts  > 0 and $promotion_goods['name']) {
                $save_counts = floor($new_counts / 3);
            }
            $sub =number_format($shopping_list['price'] * ($new_counts-$save_counts), 1, '.', '');
            $save = $save_counts*$shopping_list['price'];
            $org = number_format($shopping_list['price'] * $new_counts , 1, '.', '');
                Db::table('cart')
                    ->where('name', $name)
                    ->inc('count', $value)
                    ->update([
                        'subtotal' => $sub,
                        'original' => $org,
                        'free_count'=>$save_counts,
                        'save'=>$save,
                    ]);
            }
            foreach ($data_base as $data) {
                $total_shopping += $data['count'];
                $total +=$data['subtotal'];
            }
            $update=array(
                'count'=>$goods_name['count']+$value,
                'total_shopping'=> $total_shopping+$value,
                'subtotal'   => $sub ,
                'original'   => $org ,
                'total'=>$total
            );
        return $update;
        }
    public function show_goods_total(){
        $array= update_count();        echo json_encode($array);
    }
还有Ajax的代码function update_count(btn){
        // console.log($(this).data('name'));
        $.ajax({
            type: 'POST',
            url: 'http://localhost/tp5/public/update_count',
            data: {name: $(btn).data('name'),value: $(btn).data('value')},
            dataType: 'json',
            success: function (data) {
                // var text=JSON.parse(data);
                console.log(data.count);
     
            },
        })
 
}function show_goods_total(btn){
    $.ajax({
        type:'GET',
        url:'http://localhost/tp5/public/show_goods_total',
        // data: {name: $(btn).parent().find('input[type=text]').data('name'),value: $(btn).data('value')},
        success: function (data) {
            // var text=JSON.parse(data);
            console.log(11);
            $('#cart').html(text.total_shopping);
            $('#total').html(text.total+'元');
            $(btn).parent().find('input[type=text]').val(text.count);
            if(text.original == text.subtotal){
                $(btn).parent().parent().find('td[class=total]').text(text.subtotal+'元');
            }else{
                $(btn).parent().parent().find('td[class=total]').text(text.subtotal+'元(原价:'+text.original+'元)');
            }
        },
    })
 
}

解决方案 »

  1.   

    一,尝试 return json 数据
    二,考虑是否跨域了
      

  2.   

    是不是show_goods_total的data数据的问题呢?
      

  3.   

    是的,你调用有返回值那个方法的时候没有给那个方法传参数,
    public function show_goods_total(){
            $value = $_POST['value'];
            $name = $_POST['name'];
            $array= update_count($value,$name);
            echo json_encode($array);
        }public function  update_count ($value,$name)
    {
            //
    }
      

  4.   

    若 js 的 show_goods_total 函数最终执行的是 php 的 show_goods_total 方法
    那么你在 show_goods_total 方法中调用 update_count 方法是错误的!
    因为 update_count 是用 $_POST 做参数字体的
    而 show_goods_total 只能得到 $_GET(type:'GET')调试是请打开 php 的错误显示功能,不要自己跟自己过不去
      

  5.   

    那意思是show_goods_total的ajax应该把数据库数据传到PHP端的show_goods_total函数吧
      

  6.   

    public function  update_count ()
        {
            $value = $_POST['value'];
            $name = $_POST['name'];
        再根据 $value 和 $name 查询数据库并返回结果    $.ajax({
            type:'GET',
            url:'http://localhost/tp5/public/show_goods_total', 
    以 GET 方式调用 php 的 show_goods_total 进而调用 update_count
    并不存在 $_POST,所以不会有数据返回
      

  7.   

    就目前这个感觉给方法update_count ()传参比较简单,在其他方法中调用