Reputation: 61
I am trying to send from Laravel a response to an AJAX post request.
public function infoRoute(Request $request)
{
// Get info
$ship_id = $request->ship_id;
$startDate = $request->datepicker_start;
$endDate = $request->datepicker_end;
// Get all the locations between those dates
$routeArray = $this->measurementRepository->getCoordinates($ship_id, $startDate, $endDate);
$ship = $this->shipRepository->getShipForId($ship_id);
$info = $this->createRouteArrayForShip($ship, $routeArray);
if($request->ajax()) {
return response()->json(json_encode($info));
}
}
protected function createRouteArrayForShip($ship, $routeArray)
{
$info['type'] = "showRoute";
$index = 0;
foreach($routeArray as $coordinates)
{
$info['info']['route']['loc'. $index] = $coordinates;
$index++;
}
$info['info']['shipInfo'] = $ship;
//dd($info);
return $info;
}
When I receive the information and process it with jQuery, everything shows except from the route, that is empty.
Thank you,
Upvotes: 1
Views: 3141
Reputation: 296
The response()->json()
method converts the given array into JSON using the json_encode()
PHP function behind the scene.
Therefor you should remove your json_encode()
from inside the response()->json()
call.
Basically it should look like this
return response()->json($info);
Upvotes: 3