duanlu1959 2014-10-31 08:22
浏览 30

数据库连接无法在ajax call-laravel中工作

My javascript is:

$( "#exerciseFetch" ).change(function( event ) {   //alert("super");
    // var _token = $( this ).find( 'input[name=_token]' ).val()

    var mytoken = $('#mytoken').attr('value');
    ajaxContent = [  
        {"_token": mytoken},
        {"val"  : $( "#exerciseFetch" ).val()}                            
    ]
    $.ajax({
        type: "POST",
        url: 'exerciseFetchAjaxCall', 
        data: { ajaxContent }
        // data: { "_token": mytoken }
    }).done(function( msg ) {
        alert( msg );
        var reqContent = msg;
        $("#dynamic").html(reqContent);
    });
});

my route:

Route::group(array('prefix' => 'admin', 'namespace' => 'App\Controllers\Admin'), function()
{
    Route::post('exerciseFetchAjaxCall', 'ExerciseController@exerciseAjaxCall');
});

my controller code:

public function exerciseAjaxCall(){
    $row = $_POST['ajaxContent'];

    // $row[1]['val']

    $muscles = Muscular::whereIn('Status',array(1))->orderBy('id', 'desc')->paginate(10);

    // return    $this->content = View::make('admin.exercise.displayMusclarGroupAjax',array('row' => $row ));
    return $this->content = View::make('admin.exercise.displayMusclarGroupAjax',array('muscles' => $muscles ));
}

my view code:

@if($muscles && count($muscles) > 0)
    @foreach($muscles as $muscular)
    <?php

        $icons = ["fa-icon-user", "fa-icon-user", "fa-icon-user inactive"];    
        $typeArray = [
            '1'=> 'Resistance',
            '2'=> 'Cardio'
        ];
        $typeArray[3] = 'Resistance & Cardio';

    ?>
    <tr>
        <td data-title="muscular_name">{{ ucfirst($muscular->muscular_name) }}</td>
        <td data-title="muscular_type">{{ $typeArray[$muscular->type]}}</td>                        
    </tr>
    <?php
        $i++;
    ?>
    @endforeach
@endif
@if(isset($row) && count($row) > 0)
    <?print_r($row);?>
@endif

If I comment out the row contents and comment the muscles content in controller the code returns the values passed but the contents of the muscles are not displayed if uncommented.

I have displayed the muscle contents with the same code in its listing page (but without ajax call) dont know why its not working as I am new to laravel.

Any suggestions would be helpful...

  • 写回答

1条回答 默认 最新

  • dow72046 2014-10-31 09:59
    关注

    You have syntax error in ajax script, Try this simple $.post() api to make ajax call

     $( "#exerciseFetch" ).change(function( event ) {   //alert("super");
        // var _token = $( this ).find( 'input[name=_token]' ).val()
    
        var mytoken = $('#mytoken').attr('value'),
            ajaxContent = {
               "_token" : mytoken,
               "val"  : $( "#exerciseFetch" ).val()
            };
    
        //make ajax call 
        $.post( "exerciseFetchAjaxCall", ajaxContent ).done(
            function( msg ) {
                alert( msg );
                var reqContent = msg;
                $("#dynamic").html(reqContent);
            }
        );
    });
    

    Above should do, If nothing is returning from DB please try dd() in controller to see the returned result

    //are you sure `Status` is not in small case 
    $muscles = Muscular::whereIn('Status',array(1))->orderBy('id', 'desc')->paginate(10);
    
    dd($muscles);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器