我正在嘗試單擊一個按鈕并將上個月的資料加載到螢屏上。最初它通過當前月份和年份并且頁面加載作業正常。由于導航按鈕單擊可以在任何月份完成,因此它會計算新月份并傳遞給相同的方法。但是每次按鈕單擊后的頁面加載僅回傳回應,不會在螢屏上顯示資料。你能幫我找出導致這個問題的原因并解決這個問題。這是我的代碼片段。
jQuery
$(".month-nav-left").click(function(e){
e.preventDefault();
// currently hard coded - just to check
var month = 8;
var year = 2024;
var monthDir = "mBack";
$.ajax({
type:'POST',
url:"{{ route('monthBack') }}",
data:{month:month, year:year, monthDir:monthDir},
success:function(data){
// alert(data.success);
// console.log(data);
}
});
路線
use App\Http\Controllers\CalendarSetupController;
Route::get('/', [CalendarSetupController::class, 'index']);
Route::get('/{year}/{month}', [CalendarSetupController::class, 'monthToDisplay'])->name('selectCalendar');
Route::post('/mback', [CalendarSetupController::class, 'selectMonth'])->name('monthBack');
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class CalendarSetupController extends Controller
{
public function index()
{
$month = (int)date('m');
$year = (int)date('Y');
// This displays data correctly on initial load.
return \Redirect::route('selectCalendar', [$year, $month]);
}
public function monthToDisplay()
{
$year = request()->segment(1);
$month = request()->segment(2);
$list=array();
$month=(date("F", mktime(12, 0, 0, $month, 1, $year)));
return View::make('mainPage', ['date' => $list, 'month' => $month, 'year' => $year]);
}
function selectMonth(Request $request)
{
$input = $request->all();
$month = $input["month"];
$year = $input["year"];
switch($input["monthDir"])
{
case "mBack":
$month = (int)strftime('%m', strtotime(' 1 month', mktime(0, 0, 0, $month, 1, $year)));
break;
}
// This only have the correct view in the response, but does not load on the screen.
return \Redirect::route('selectCalendar', [$year, $month]);
}
}
謝謝你。
uj5u.com熱心網友回復:
monthBack是一個 Web 路由,所有針對它的請求都將通過VerifyCsrfToken中間件,該中間件會檢查有效令牌。
由于您的 ajax 請求不包含有效的_token密鑰,它將被拒絕(未經授權,您可以在實時artisan serve日志中看到 HTTP 答案)。
如果您沒有編輯您的 HTML 布局,則標頭已經包含一個有效的標記。
<meta name="csrf-token" content="{{ csrf_token() }}" />
以下是如何將該令牌添加到您的 POST 請求中:
$.ajax({
type: "POST",
url: "{{ route('monthBack') }}",
data: {
month:month, year:year, monthDir:monthDir,
_token:$('meta[name="csrf-token"]').attr('content')
},
success: success,
dataType: dataType
});
或添加
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
請注意,令牌在有限的時間內保持有效,如果您有一段時間不重繪 頁面,則該按鈕將不起作用。
uj5u.com熱心網友回復:
我在 javascript 代碼中添加了一行重定向到所需的 url。我不確定這是否是最佳實踐,但它確實按預期進行。感謝您的時間。
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".month-nav-left").click(function(e){
e.preventDefault();
// currently hard coded - just to check
var month = 8;
var year = 2024;
var monthDir = "mBack";
$.ajax({
type:'POST',
url:"{{ route('monthBack') }}",
data:{month:month, year:year, monthDir:monthDir},
success:function(data){
// alert(data.success);
// console.log(data);
}
});
window.location.replace("http://127.0.0.1:8000/" year "/" month);
});
uj5u.com熱心網友回復:
如果您嘗試使用輔助函式redirect()->route('route.name', 'data' => $data);而不是使用\Redirect::route()靜態方法會發生什么?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428457.html
下一篇:添加到購物車
