I'm using Laravel 5.1. I try to get some data in JSON format through the controller, but it return html instead of 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() !!}
Ajax in schedule.form
$.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 function in controller
public function loadRoute(Request $request){
$routeID=$request->get('routeID');
$station=Station::where('route_id',$routeID)->get()->toArray();
echo json_encode($station);
}
Edit
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');
});
Both create and edit page share the same schedule.form, but the JSON data is returned successfully in create page only, for edit page it return html instead of JSON, and I get this error in console (Uncaught SyntaxError: Unexpected token < in JSON at position 0)
Both page using the same form, but why it doesn't work when comes to edit page?
Ajax in schedule.formsection in a blade file or a javascript file?