dongzha5934
2018-08-28 12:17
浏览 78
已采纳

Laravel ajax没有返回数据

I am having trouble retrieving data from Controller using ajax this is my code

the trigger

<select id="course_select" name="course_select" class="form-control" required autofocus value="{{old('course_select')}}" onchange="getCourse()">

The my ajax section

function getCourse(){
  var id=document.getElementById('course_select').value;
  alert(id);
  $.ajax({
    type:'get',
    url:'getCourseLessons/'+id,
    datatype:'json',
    success:function(data){
      console.log(data);
      alert(data.lessons);
    }
  });
}

and my controller

public function getCourseLessons($id){
      $lessons = DB::table('lessons')->select('id','Lesson_name')->where('course_id','=',$id)->get();
      return response()->json(array('lessons'=>$lessons));
}

Results console.log(data)

console.log(data)
ReferenceError: data is not defined[Learn More]

My Routes.

Route::get('/getcourselessons/{id}','AdminController@getCourseLessons');

I will appreciate all the help i can get.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douzhaxian1267 2018-08-28 12:59
    已采纳

    First of all please check the route you used in Route/Web.php it should be like this

    Route::get('getCourseLessons/{id}','YourController@getCourseLessons');
    

    Check the controller.

    public function getCourseLessons($id){
      $lessons = \DB::table('lessons')->select('id','Lesson_name')->where('course_id',$id)->get();
      return response()->json(array('lessons'=>$lessons));
    }
    

    As per your ajax code is ook but you can try this also.

    function getCourse(){
      var id=$('#course_select').val();
      alert(id);
         $.ajax({
           type:'get',
           url:'{{url('getCourseLessons')}}/'+id,
           datatype:'json',
           success:function(data){
               console.log(data);
               alert(data.lessons);
            }
         });
     }
    
    已采纳该答案
    打赏 评论
  • dqdz6464 2018-08-28 12:37
    <select id="course_select" name="course_select" class="form-control"  onchange="getCourse()">
       <option>hi</option>
       <option>hello</option>
    </select>
    

    It seems that there is problem in your select as the above code will output check the following

     value="{{old('course_select')}}"
    

    and one more thing I can't see CSRF

    Add somewhere in your script

    $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
    
    打赏 评论
  • douli6605 2018-08-28 12:40

    try this bro change you url or route

    function getCourse(){
     var id=document.getElementById('course_select').value;
     var url = '{{ url('getCourseLessons/')}}/'+id;
     alert(id);
     $.ajax({
     type:'get',
     url:url,
     datatype:'json',
      success:function(data){
       console.log(data);
       alert(data.lessons);
       }
      });
    }
    

    change your route

    Route::get('getcourselessons/{id}','AdminController@getCourseLessons');
    
    打赏 评论
  • duandao2306 2018-08-28 12:50

    1- cheek your route

    Route::get('/getCourseLessons/{id}','YourController@getCourseLessons');
    

    2- change your Ajax attributes

    function getCourse(){
      var id=document.getElementById('course_select').value;
      alert(id);
      $.ajax({
        url: '{{ url("getCourseLessons") }}',
        type:'get',
        datatype:'json',
         data: {
                "id": id,
                "_token": "{{ csrf_token() }}"
              },
        success:function(data){
          console.log(data);
          alert(data.lessons);
        }
      });
    }
    
    打赏 评论

相关推荐 更多相似问题