2017-02-09 9 views
0

私はクライアントを選択でき、次の選択フォームはクライアントのすべてのアイテムを表示しますが、何らかの理由でそれに伴うトラブル。ブレードビューでJSONレスポンスを取得する - Laravel 5.3

これは私のroute.php

Route::resource('bills', 'BillsController'); 
Route::get('bills/items','[email protected]'); 

であり、これは私のBillsController

private function items($request) { 
    try { 
     if ($request) { 
      $id = $request->client_id; 
      $items = Item::where('client_id', $id)->get()->pluck('code', 'id'); 

      return redirect()->json($items); 
     } else { 
      \Session::flash('error_message', 'Ups! Hemos tenido un error.'); 
      return redirect()->back(); 
     } 
    } catch (Exception $e) { 
     \Session::flash('error_message', 'Ups! Hemos tenido un error.'); 
      return redirect()->back(); 
    } 
} 

であり、これは私のブレードビューでスクリプトです:

<script> 
$(document).ready(function() { 

    $('#clients').change(function() { 
     var $client_id = $('#clients').val(); 
     console.log($client_id); 
     var $url = '{{ url('MyAdmin/bills/items') }}'; 
     console.log($url); 
     $.getJSON($url, {'client_id': $client_id}, function(resp) { 
      console.log(resp); //For some reason here the "resp" is not working 
      $.each(resp, function(key, answer) { 
       $('#items').append('<option value="'+answer.id+'">'+answer.code+'</option>'); 
      }); 
     }); 
    }); 
}); 

何か案が?どうもありがとう!

答えて

0

実際には} catch (\Exception $e)に入っているためです。しかし、ここではJSON応答を返していません。あなたはリダイレクトしています。

問題は、redirect()オブジェクトに->json()メソッドが含まれていませんが、response()オブジェクトが含まれているという問題があります。

return response()->json($items); 

また、お使いのエラーハンドラでは、応答422エラーコードを返すので、それはあなたの約束に.error()をトリガーウィル:だから、それを変更します。私は試した

return response(422)->json([$e->getMessage()]); 
+0

、何が起こる:( –

+0

@CarlosOrtegaアクセスブラウザに直接リンクを動作していない – Ohgodwhy

+0

'Route.phpライン333でReflectionException:。?メソッドのApp \のHttp \コントローラ\バックエンドの\ BillsControllerを: :show()does not exist '私はこれを持っています –

関連する問題