Kunal Parekh
Kunal Parekh

Reputation: 380

Laravel - Ajax call not responding

I am trying to Post the data from the dropdown box and sent the request using Ajax. It is working fine on the localhost but its not working properly on the shared-hosting. I have 3 dropboxes and the value changes according to the values selected from its parent dropbox. For Eg: Car Make (Audi) -> Car Model -> (A4, A5, Q5 etc.. audi models) ->year (19XX - 2019)

I am only able to select the Make, but I am not able to get any data from the Car Make selected.

home.blade.php

<div class="form-group">

  <select   class="form-control dynamic"  name="Make"  id="Make" data-dependent='Model'>
    @foreach($carLists as $carMake)
    <option value="{{$carMake->Make}}">{{$carMake->Make}} </option>

    @endforeach
  </select>
</div>

<div class="form-group">

  <select   class="form-control dynamic"  name="Model" id="Model"  data-dependent='Year'>
    <option value="">Select Model</option>
  </select>
</div>


<div class="form-group">

  <select   class="form-control dynamic" name="Year" id="Year"  data-dependent='Body'>
    <option value="">Select Manufacturing year</option>
  </select>
</div>

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

 $('.dynamic').change(function(){
  if($(this).val() != '')
  {
   var select = $(this).attr("id");
   var value = $(this).val();
   var dependent = $(this).data('dependent');
   var _token = $('input[name="_token"]').val();
   $.ajax({
      url:"{{ route('pagescontroller.fetch') }}",
    method:"POST",
    data:{select:select, value:value, _token:_token, dependent:dependent},
    success:function(result)
    {
     $('#'+dependent).html(result);
    }

   })
  }
 });

 $('#Make').change(function(){
  $('#Model').val('');
  $('#Year').val('');
   $('#Make').val($(this).val());
   console.log($('#HidMake'));
 });

 $('#Model').change(function(){
  $('#Year').val('');
 });

});
</script>

PageController.php

class PagesController extends Controller
{

          function fetch(Request $request)
          {
           $select = $request->get('select');
           $value = $request->get('value');
           $dependent = $request->get('dependent');
           $data = DB::table('carLists')
             ->where($select, $value)
             ->groupBy($dependent)
             ->get();
           $output = '<option value="">Select '.ucfirst($dependent).'</option>';
           foreach($data as $row)
           {
            $output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
           }
           echo $output;
          }

Routes (web.php)

Route::post('inc/sidebar/fetch', 'PagesController@fetch')->name('pagescontroller.fetch');

I am being trying alot but no clue whats going wrong here, however, on the localhost its working fine.

thanks for the time.

Upvotes: 1

Views: 214

Answers (1)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9703

Try changing the url to

var url = APP_URL + '/inc/sidebar/fetch';

and set header

 headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },

instead of

 var _token = $('input[name="_token"]').val();

and let me know, the result you are getting for this.

Upvotes: 1

Related Questions