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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50926807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:49:35  来源:igfitidea点击:

Laravel - Ajax (GET) : How to send json data from controller back to view

phpjqueryajaxlaravel

提问by Swiggity

I'm facing a problem here. I am trying to send the specific date to the controller (to make a where clause and send that back to the view) but I think it isn't even needed with a controller since you can also put a where clause in the view.

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

However, both parts are kinda impossible for me, tried everything but no result.

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

my ajax/view code...

我的 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);works, it outputs the date of a clicked day and what I need is that output (from JS) to Laravel (PHP) where clause to fetch data from the database.

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

my controller code...

我的控制器代码...

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);
}

my route:

我的路线:

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

and then in my view, I have something like this, but obviously, it doesn't work since the variable doesn't get sent... (no errors)

然后在我看来,我有这样的事情,但显然,它不起作用,因为变量没有被发送......(没有错误)

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

I hope that someone can help me out here...

我希望有人能在这里帮助我...

Note: I don't have that good experience in JSON/AJAX/Laravel, I'm a beginner.

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

回答by H H

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

首先,在您的 AJAX 请求中,您没有指定route(或 url)将数据发送到:

$.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 routeto your routes/web.phpfile. Something like:

您需要routeroutes/web.php文件中添加一个。就像是:

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

Secondly, in your roostermethod you should be passing back JSON

其次,在你的rooster方法中,你应该回传JSON

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

If the roostermethod 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

如果该rooster方法不是您应该向其发送 AJAX 请求的方法,只需确保它应该使用的方法返回 JSON 响应

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

然后,您将需要使用 JS 使用response给定的返回来更新视图

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/

也许使用基于 JS 的模板,例如https://github.com/janl/mustache.js/

回答by Kul

you are missing urlparameter for ajax.

您缺少urlajax 的参数。

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

提示:您可以创建具有 where 子句 html 逻辑的新视图,并将其包含在主视图中 .. 并view('your-view-path')->render()在您的rooster()内部使用渲染它。和

Feel free to ask anything .. cheers.

随意问任何事情..干杯。