2017-03-03 5 views
2

私は入力値をLaravelに入力して入力変更を投稿し、一致するすべてのデータを含む配列をクエリに返そうとしています。しかし、私は常に空の配列を受け取ります。私はそれを稼働させるためにたくさんのことを試みましたが、それは成功しませんでした。Laravelはajax投稿要求後に空の文字列を返します

コンソールは言う:

オブジェクト レベル: "[]" 状況:オブジェクト プロト:matchvalueはコンソール内の値を有する

オブジェクト、クエリが行う仕事私が変更されたときます$ this- >をデータベースにある文字列に入力します。問題の根は$ requestと$ request->型ですが、解決策は見つけられません。

私は本当にこの問題のラインの終わりに達しているので、私はすべての助けに感謝します。前もって感謝します。

$(document).ready(function() { 
 
     $("#question_type input").on('change', function postinput() { 
 
     var matchvalue = $(this).val(); // this.value 
 
     $.ajax({ 
 
      type: 'POST', 
 
      url: "{{route('get-levels')}}", 
 
      dataType: "json", 
 
      data: matchvalue 
 
     }).done(function(response) { 
 
      console.log(matchvalue); 
 
      console.log(response); 
 
      $('#question_type').append(response.levels); 
 
      console.log(response.levels); 
 
     }); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<fieldset class="form-group form_section" id="question_type"> 
 
    <h5>Selecteer in type vraag</h5> 
 
    <div class="form-check"> 
 
    <label class="form-check-label"> 
 
     <input class="form-check-input input" type="radio" name="type" id="exam" value="exam"> 
 
     Examenvraag 
 
    </label> 
 
    </div> 
 
    <div class="form-check"> 
 
    <label class="form-check-label"> 
 
     <input class="form-check-input input" type="radio" name="type" id="practice" value="practice"> 
 
     Oefenvraag 
 
    </label> 
 
    </div> 
 
</fieldset> 
 

 

 
ROUTE 
 

 
Route::post('/selectQuestion/selection/levels', '[email protected]')->name('get-levels')->middleware('auth'); 
 

 

 

 
CONTROLLER 
 

 
public function getLevels(Request $request){ 
 
     
 
\t \t $this->_type = $request->type; 
 
     
 
\t \t $levels = 
 
\t \t \t Question:: 
 
\t \t \t \t distinct() 
 
\t    ->where('type', $this->_type) 
 
\t    ->orderBy('level') 
 
\t    ->get(['level']); 
 

 
\t \t return response()->json(['levels' => strip_tags($levels), 'status' => $request]); 
 
\t }

+0

([ 'レベル' => strip_tags($レベル)、 '状態' => $リクエスト])エコーjson_encodeをしてみてください。 – Omi

+0

@omi同じ結果unfortunaly :( –

+0

あなたのjQuery ajaxの呼び出しでは、 'data'はキー/値のペア文字列(クエリ文字列)内にある必要がありますか、オブジェクト/配列として渡され、jQueryによってクエリ文字列に変換されます意味は 'type = yourvalue'のようなものでなければなりません –

答えて

1

JavaScriptコードでdata属性は次のようになります。

data: { 'type': matchvalue } 

変更行:

$this->_type = $request->type; 

へ:

$this->_type = $request->input('type'); 

$this->_typeにフォームからキャプチャがキャプチャされていることを確認します。

またにクエリを変更してみてください:

$levels = Question::distinct()->where('type', $this->_type)->orderBy('level')->get()->lists('level'); 
+0

これはうまくいきます! –

関連する問題