【发布时间】:2019-03-05 01:21:08
【问题描述】:
我正在使用 Laravel 5.1。我尝试通过控制器获取一些 JSON 格式的数据,但它返回的是 html 而不是 json。
create.blade.php
{!! Form::open(['url' => 'schedule']) !!}
@include('schedule.form',['submitButtonText'=>'Submit'])
{!! Form::close() !!}
edit.blade.php
{!! Form::model($schedule,['method'=>'PATCH','url' => 'schedule/'. $schedule->scheduleID ]) !!}
@include('schedule.form',['submitButtonText'=>'Update'])
{!! Form::close() !!}
schedule.form 中的 Ajax
$.post('../schedule/loadRoute',{routeID:routeID},function(r){
console.log(r);
$.each(JSON.parse(r), function(key, value){
coordinates.push(new google.maps.LatLng(value.lat,value.lon));
if(value.is_station==1){
addMarker(new google.maps.LatLng(value.lat,value.lon),value.name);
}
});
clearMap();
});
控制器中的loadRoute函数
public function loadRoute(Request $request){
$routeID=$request->get('routeID');
$station=Station::where('route_id',$routeID)->get()->toArray();
echo json_encode($station);
}
编辑
routes.php
Route::group(
['middleware' => ['web']],
function () {
Route::auth();
Route::get('schedule/getScheduleByRouteID/{routeID}', 'ScheduleController@getScheduleByRouteID');
Route::resource('schedule', 'ScheduleController', ['except' => ['destroy', 'show']]);
Route::post('schedule/loadRoute','ScheduleController@loadRoute');
});
创建和编辑页面共享相同的 schedule.form,但 JSON 数据仅在创建页面中成功返回,对于编辑页面,它返回 html 而不是 JSON,我在控制台中收到此错误(未捕获 SyntaxError:意外令牌
两个页面使用相同的表单,但为什么在编辑页面时不起作用?
【问题讨论】:
-
可能是因为服务器由于某些服务器错误而返回 HTML,或者它实际上应该这样做而您访问不正确
-
Ajax in schedule.form部分是在刀片文件还是 javascript 文件中? -
它在刀片文件脚本部分中