PHP Laravel : Ajax couldn't retrieve data (No properties)

标签 php mysql ajax laravel laravel-5

我正在构建一个 laravel 应用程序,我想在其中使用 ajax Controller 生成一些报告。在 MySQL 中使用相同的原始查询我可以看到数据但是使用 ajax 我无法在我的 View 页面中显示。 在控制台中显示 [] No Properties

enter image description here

我不明白为什么会显示上述错误。如果有人发现问题所在,请帮我找出来。谢谢。

这是我用来从数据库中检索数据的 Controller :

  public function ajax_view_schedule(Request $request)
    {

          $dept_id= $request->Input(['dept_id']);
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE( CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule
FROM departments join courses on departments.id = courses.department_id
left join allocate_rooms on allocate_rooms.course_id=courses.id 
left join rooms on allocate_rooms.room_id=rooms.id
left join days on allocate_rooms.day_id=days.id WHERE departments.id='.$dept_id.'"));
        return \Response::json($schedule);  
    }

这是带有ajax代码的 View 页面:

<div class="container" >
        <h3> View Class Schedule and Room Allocation Information </h3>

    <div class="form-group">
        <label for="">Department</label>
        <select class="form-control input-sm" required id="department" name="department_id" >
        <option>Select a Department</option>
        @foreach($department as $row)
        <option value="{{$row->id}}">{{$row->name}}</option>
        @endforeach
        </select>
    </div>   



    <table  class="table table-striped table-bordered"  id="example">
    <thead>
      <tr>

        <td>Course Code</td>
        <td>Name</td>
        <td>Schedule Info</td>                      
      </tr>
    </thead>
    <tbody>

   </tbody>
    </table>        
    </div>

    <script type="text/javascript">
     $('#department').on('change',function(e){               
       var dept_id = $('#department option:selected').attr('value');

      $.ajaxSetup({
                  headers: {
                      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                  });

               $.ajax({ 
                  type: "POST", 
                  url : "{{url('ajax-view-schedule')}}",
                  data:{dept_id:dept_id},
                success : function(data) { 
                      var $tbody = $('#example tbody').empty();                  
                    $.each(data,function(index,subcatObj){                    
                    $tbody.append('<tr><td class="code">' + subcatObj.c_code + '</td><td class="course_name">' + subcatObj.c_name + '</td><td class="schedule">' + subcatObj.schedule + '</td></tr>');
                        });
                     } 
              });     
        });
    </script>   

最佳答案

你实际上是用双引号引起来的,所以 php 会插入你的变量

  public function ajax_view_schedule(Request $request)
    {

          $dept_id= $request->Input(['dept_id']);
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE( CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule
FROM departments join courses on departments.id = courses.department_id
left join allocate_rooms on allocate_rooms.course_id=courses.id 
left join rooms on allocate_rooms.room_id=rooms.id
left join days on allocate_rooms.day_id=days.id WHERE departments.id='$dept_id'"));//remove dot from .$dept_id.
        return \Response::json($schedule);  
    }

关于PHP Laravel : Ajax couldn't retrieve data (No properties),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247476/

相关文章:

php - WooCommerce 产品图像缩放选项的可用参数详细信息

javascript - backbone.js + jquery-mockjax : fetch success handler doesn't work

php CLI 参数问题

mysql - 如何使用fat-free ORM将数据插入到仅id表中?

php - mysql 或 perl 函数匹配 Asterisk 拨号方案模式

mysql - 有没有办法在所有数据库的sql server中作为整数工作

php - 在php中设置自动字段值

javascript - Laravel 中的简单 Ajax

php - 在通过电子邮件发送用户输入的数据之前,我是否需要对其进行清理

php - 使用 htaccess 重写 Url 会使搜索引擎发现网站吗?