vendredi 3 mars 2017

Laravel returns empty string after ajax post request

I am trying to post a input value live to Laravel on input change and return an array with all matching data to the query. However I receive an empty array all the time. I have tried alot of things to get it working but without succes.

console says:

Object levels: "[]" status: Object proto: Object

matchvalue has a value in console, the query does work when I change $this->type to a string that is in the database. So the root of the problem is the $request and $request->type, but I cant find a solution.

I would appreciate all the help, because I have really reached the end of the line on this problem. Thanks in advance.

$(document).ready(function() {
      $("#question_type input").on('change', function postinput() {
        var matchvalue = $(this).val(); // this.value
        $.ajax({
          type: 'POST',
          url: "",
          dataType: "json",
          data: matchvalue
        }).done(function(response) {
          console.log(matchvalue);
          console.log(response);
          $('#question_type').append(response.levels);
          console.log(response.levels);
        });
      });
<script src="http://ift.tt/1oMJErh"></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', 'SelectionController@getLevels')->name('get-levels')->middleware('auth');



CONTROLLER

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

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


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire