Laravel - Ajax(GET):如何将控制器中的json数据发送回视图

时间:2018-06-19 11:01:00

标签: php jquery ajax laravel

我在这里遇到问题。我试图将特定日期发送到控制器(制作一个where子句并将其发送回视图)但我认为甚至不需要控制器,因为你也可以在视图中放置一个where子句

然而,这两个部分对我来说都是不可能的,尝试了一切但没有结果。

我的ajax /查看代码......

$('.dates').click(function(){
    var date = $(this).attr('value');

    $.ajax({
        type: 'GET',
        data: {
            date: date
        },
        success: function(data){
            console.log(data.data);
        },
        error: function(xhr){
            console.log(xhr.responseText);
        }
    });
});

console.log(data.data);有效,它输出点击日的日期,我需要的是输出(从JS)到Laravel(PHP)where子句从数据库中获取数据。

我的控制器代码......

public function rooster(Request $request)
{
    $currentUser = Auth::user();
    $schedules = Schedule::all();

    $data = $request->date;
    if($data){
        return response()->json(['msg'=>'Updated Successfully', 'success'=>true, 'data'=>$data]);
    }

    $dataa = $data;
    return view('pages.klant.rooster')->withDataa($dataa)->withSchedules($schedules);
}

我的路线:

Route::get('klant/rooster', 'KlantController@rooster');
Route::get('klant/kalender', 'KlantController@getData');

然后在我看来,我有类似的东西,但很明显,由于变量没有被发送,所以它不起作用......(没有错误)

@foreach ($schedules->where('datum', $dataa) as $value)
    {{ $value->user->name }}
@endforeach

我希望有人可以帮助我...

注意:我在JSON / AJAX / Laravel中没有那么好的经验,我是初学者。

3 个答案:

答案 0 :(得分:2)

Firstly, in your AJAX request you haven't specified the route (or url) to send the data to:

$.ajax({
    type: 'GET',
    url: '/rooster/filter',
    data: {
        date: date
    },
    success: function(data){
        console.log(data.data);
    },
    error: function(xhr){
        console.log(xhr.responseText);
    }
});

You will need to add a route to your routes/web.php file. Something like:

 Route::get('/rooster/filter', 'MyController@rooster');

Secondly, in your rooster method you should be passing back JSON

return response()->json([
    'data' => $data,
    'schedules' => $schedules
]);

If the rooster method is not the one you should be sending the AJAX request to, just make sure the method it should go to returns a JSON response

You will then need to use JS to update the view using the response given back in

success: function(data){
    console.log(data.data);
    console.log(data.schedules);
},

Maybe use a JS based templating such as https://github.com/janl/mustache.js/

答案 1 :(得分:0)

you are missing url parameter for ajax.

For tips : you can make new view having your where clause html logic and include it in main view .. and render it using view('your-view-path')->render() inside your rooster(). And

Feel free to ask anything .. cheers.

答案 2 :(得分:0)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Text("test")
    );
  }
$('.butto').click(function(){
    var date = $(this).attr("data-id");
    //var date = $(this).attr('value');
$.ajax({
  url: '/ajax/GetContent',
  data: { date: date },
  type: "GET",  
  success: function(data){ $data = $(data);      
  $('#clear').hide().html($data).show();
   }
  });
});

//controller
public function index3(Request $request){
        //$search = 'fffffffff';
        //$search = $request->date;
        // $search = $request->input('search');
        $user = Auth::user();
        $id = $request->date;
        //$search  = moneyuser::where($where)->first();
        $search = moneyuser::where('name', $user->name)->where('email', $user->email)->where('id', $id)->paginate(3);

        return view('student.lis')->with('search', $search);
    }
    
    // Rout
    // change Page by Ajax
Route::get('/ajax/GetContent', array(
    'uses'  =>  'StudentController@index3'
  ));