我正在尝试在laravel中执行ajax帖子。我有一个控制器“show_freeCars”awich从ajax post获取一些值并执行查询以返回结果。我有这个:
public function show_freeCars() {
$starttime = Input::get('starttime');
$endtime = Input::get('endtime');
$enddate = Input::get('enddate');
$startdate = Input::get('startdate');
$classes = Input::get('classes');
$free_cars = DB::select('SELECT * FROM cars WHERE id NOT IN (select distinct c.id from cars as c '
. 'left join rentals as r on c.id = r.vehicle '
. 'where STARTTIME="' . $startdate . ' ' . $starttime . '" '
. 'AND ENDTIME="' . $enddate . ' ' . $endtime . '" '
. 'AND (r.status="REZERVATION" OR r.status="CHECK_OUT") '
. 'order by c.id) AND class= "' . $classes . '" ');
if (Request::ajax()) {
$response_values = array(
'success' => 1,
'values' => $free_cars,
);
return Response::json($response_values);
} else {
dd('error');
}
}
和javascript
jQuery('#form-reservation').submit(function()
{
var startdate = $('input[name$="startdate"]').val();
var starttime = $('input[name$="starttime"]').val();
var enddate = $('input[name$="enddate"]').val();
var endtime = $('input[name$="endtime"]').val();
var classes = $('input[name$="classes"]').val();
var url = $(this).attr("action");
jQuery.ajax({
url: url,
type: "post",
data: {
startdate: startdate,
starttime: starttime,
enddate: enddate,
endtime: endtime,
classes: classes
},
datatype: "json",
beforeSend: function()
{
jQuery('#ajax-loading').show();
}
})
.done(function(data)
{
$('#cars_table').empty()
if (data.success === 1)
{
var arr = data.values;
alert(arr);
}
else {
window.location = data.redirect_to;
}
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('Invalid Data');
});
return false;
});
警告空。但是,如果我从控制器中删除行$classes = Input::get('classes');
和AND class= "' . $classes . '" '
,则会显示结果。查询没问题,因为我用dd($ free_cars)测试了它,它显示了汽车列表。只是有些东西被ajax返回值搞砸了。为什么它返回null?