2016-05-05 67 views
0

我試圖從Laravel發送對AJAX發佈請求的響應。AJAX發送陣列作爲來自Laravel的響應

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; 
    } 

當我收到信息並使用jQuery處理它時,除路由之外,所有內容都顯示爲空。

謝謝

+0

如果您使用瀏覽器的開發者工具..什麼數據,你看回來的迴應? – Dale

+0

你有一個多維數組在那裏.. –

+0

試着把它作爲JSON返回 – kejsu

回答

2

response()->json()方法給定的數組到JSON使用場景背後的json_encode() PHP函數轉換。 爲此,您應該從response()->json()呼叫中刪除您的json_encode()

基本上它應該是這樣的

return response()->json($info); 
+0

謝謝!我已經做到了,但仍然無法正常工作。我認爲這與'$ info'不包含路線中的座標有關。它被髮送空。如果我在返回響應之前執行'dd($ info)',則路由爲空。但是當我調用函數createRouteArrayForShip時,它實際上將記錄保存在數組中 –