如何从控制器发送json数据进行查看?

时间:2020-02-20 15:57:44

标签: javascript jquery ajax laravel

   public function getSectionsForClass(Request $request ,$id )
    {
        $section = Section::all()->where('clas_id',$id);
         return  response()->json($section);

    }

我需要将此json发送到表中的视图中,例如

@foreach($section as $sec)  
     {{$sec->section}}
      {{$sec->capacity}}
        {{$sec->teacher}}
      {{$sec->class}}

  @endforeach

这是我的ajax代码,我已在其中发送ID和URL

 <script type="text/javascript">
         $('#select_id').change(function(){
             // alert('hello');
             var cid = $(this).val();
             if(cid){
                 $.ajax({
                     dataType: "json",
                     url: 'section/index/'+cid,
                     //data: {'class': cid},
                     type:"GET",
                     success: function(response){
                         console.log ((response));

                     },error: (error) => {
                         console.log(JSON.stringify(error));
                   }
              });
             }
         });

     </script>

在途中

Route::get('admin/section/index/{id}','SectionController@getSectionsForClass');

预先感谢我能得到答案

2 个答案:

答案 0 :(得分:0)

根据Laravel文档,您可以直接从您的方法发送set Session = CreateObject("Redemption.RDOSession") Session.MAPIOBJECT = Application.Session.MAPIOBJECT set AdrrEntries = Session.AddressBook.ResolveNameEx("john") for each AE in AdrrEntries MsgBox(AE.Name) next 响应

json方法将自动将json标头设置为 Content-Type,并使用将指定的数组转换为JSON application/json PHP函数:

json_encode

然后您可以通过将ajax成功创建动态表,将其附加到特定ID的表中,例如

return response()->json([
    // You can pass your array values here
]);

Laravel -> Http Responses -> Json Responses

答案 1 :(得分:0)

当前,您将返回查询生成器的实例作为json响应。您尚未执行查询。

要从查询中获取结果,您需要实际执行查询。在末尾添加select t.*, (case when td_day_of_week(cast(extract(year from DATE_SPECIFIED) || '-01-01' as date) ) = 1 then cast(extract(year from DATE_SPECIFIED) + '-01-02' as date) else extract(year from DATE_SPECIFIED) + 10 - cast(td_day_of_week(cast(extract(year from DATE_SPECIFIED) || '-01-01') as date) from t;

get()

当然,如果您有很多记录,可以在分页列表中更好地进行管理,但是您知道了。

相关问题