【发布时间】:2016-09-11 20:42:15
【问题描述】:
我正在构建一个 laravel 应用程序,我想在其中使用 ajax 控制器生成一些报告。在 MySQL 中使用相同的原始查询,我可以看到数据,但使用 ajax 我无法在我的视图页面中显示。
在控制台中显示[] No Properties
我不明白为什么它会显示上述错误。如果有人发现有什么问题,请帮我找出来。谢谢。
这是我用来从数据库中检索数据的控制器:
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 代码的视图页面:
<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>
【问题讨论】:
-
可以发一下开发者工具网面板截图吗?
-
当您导航到 website.url/ajax-view-schedule 时,您会看到什么?
-
我已经上传了截图,这是我的查看页面
标签: php mysql ajax laravel laravel-5