【问题标题】:how can i send multiple data from controller to page html using ajax如何使用ajax将多个数据从控制器发送到页面html
【发布时间】:2019-10-05 06:25:45
【问题描述】:

在我的页面中,我使用 ajax 从 db 获取数据,所以在我的控制器中,我检索两个数据,当我将它们发送到页面时,我只得到一个数据

这是我使用的方法 ajax

$.ajax({
    type:'get',
    url:'{!!URL::to('gestion_commandes/create/gencodesol1')!!}',
    data:{'id':type, 'id':code},
    dataType:'json',
    success:function(data){
        console.log('success');
        console.log(data);
        console.log(data.nbrdebut);
        //console.log(data.type);

      /* var codesol=data+data.nombre;
        for(y=1;y<=nbrrowsol;y++ ){
           var somme=codesol+y;
        $('#codesol'+y).val(somme);
        console.log(somme);
        }*/
    },
    error:function(){

    }
});

这是我在控制器中的功能

 public function findnaturesol1(Request $request)
    {
        $data=Commande::select('nombre')->where('code', 'LIKE', '%'.$request->id.'%')->count();
        if($data!=0){
            //$data1=Commande::select('select code from commandes ')->where('code','LIKE', '%'.$request->id.'%')->count();

           // $data=Matrice::select('nbrdebut')->where('type',$request->id)->first(); 
           //  $data=$vide1+$data2;
           // echo '$data2->$nbrdebut';
            return response()->json($data);
        }
        /*else{
            $data=DB::table('commandes')-insertGetId(['code' => 'first']);
        }*/


    }

【问题讨论】:

  • 当我只发送一个 id 时同样的问题
  • 不工作,我的问题是如何从控制器 2 中获取数据,例如使用数组作为返回响应...

标签: jquery mysql ajax laravel


【解决方案1】:

在控制器中:

public function findnaturesol1(Request $request)
    {
        $data=Commande::select('nombre')->where('code', 'LIKE', '%'.$request->id.'%')->count();
        if($data!=0){

            $data1 = Commande::select('select code from commandes ')->where('code','LIKE', '%'.$request->id.'%')->count();

            $data2 = Matrice::select('nbrdebut')->where('type',$request->id)->first(); 

            $data = [
                 'data1' => $data1,
                 'data2' => $data2
            ];
            return response()->json(['data' => $data]);
        }

    }

在js中:

$.ajax({
    type:'get',
    url:'{!!URL::to('gestion_commandes/create/gencodesol1')!!}',
    data:{'id':type, 'id':code},
    dataType:'json',
    success:function(response){
        console.log('data1::', response.data.data1);
        console.log('data2::', response.data.data2);
    },
    error:function(){

    }
});

【讨论】:

    【解决方案2】:

    这是我的代码抱歉我忘了注释标记

    public function findnaturesol1(Request $request)
        {
            $data=Commande::select('nombre')->where('code', 'LIKE', '%'.$request->id.'%')->count();
            if($data!=0){
                //$data1=Commande::select('select code from commandes ')->where('code','LIKE', '%'.$request->id.'%')->count();
    
                $data=Matrice::select('nbrdebut')->where('type',$request->id)->first(); 
               //  $data=$vide1+$data2;
               // echo '$data2->$nbrdebut';
                return response()->json($data);
            }
            /*else{
                $data=DB::table('commandes')-insertGetId(['code' => 'first']);
            }*/
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-02
      • 2020-05-10
      • 1970-01-01
      • 2017-02-01
      • 2014-05-15
      • 1970-01-01
      • 2021-09-26
      • 2021-03-25
      相关资源
      最近更新 更多