简体   繁体   English

Laravel 5 Redirect在AJAX GET网址中不起作用

[英]Laravel 5 Redirect doesn't work from AJAX GET url

I have a date picker and I put a jquery listener to it on change: 我有一个日期选择器,并在更改时放置了一个jQuery侦听器:

$('#viewmonth').datepicker({language: 'ja', autoclose: true,  minViewMode: 'months', format: $('#month_format').text(),})
.change(function() {
    let date = $(this).datepicker("getDate");
    let month = date.getMonth() + 1;
    date = date.getFullYear() + '-' + month;

    $.ajax({
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        type:'GET',
        url:'/head/sale/h_2_501/changeMonth',
        data: {'viewmonth': date}
    });
});

When the date is changed, it redirects to changeMonth function: 日期更改后,它将重定向到changeMonth函数:

public function changeMonth() {
    $viewmonth = date("Y-m-d", strtotime(Request::input('viewmonth')));
    $viewmonth = mb_convert_kana($viewmonth, 'a');

    error_log($viewmonth);

    $filter = $this->pageInfo['filter'];

    $filter['viewmonth'] = $viewmonth;

    $this->updateFilterData($filter);

    return redirect()->route('head.sale.h_2_501');
}

Inside this function, I am changing the date of the query I use to display some data. 在此函数内部,我正在更改用于显示某些数据的查询日期。 The problem is after redirecting to the index route inside changeMonth, the results display doesn't change but the query changed. 问题是重定向到changeMonth内的索引路由后,结果显示未更改,但查询已更改。 How do I also update the data displayed? 如何更新显示的数据?

First remove return redirect()->route('head.sale.h_2_501'); 首先删除return redirect()->route('head.sale.h_2_501'); from changeMonth() function. 来自changeMonth()函数。

Then add it to the AJAX success call. 然后将其添加到AJAX成功调用中。

$.ajax({
    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    type:'GET',
    url:'/head/sale/h_2_501/changeMonth',
    data: {'viewmonth': date},
    success: function (response) {

        // this is the redirect after your changeMonth() function succeed.
        window.location = '{{ route('head.sale.h_2_501') }}'
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM