【发布时间】:2018-02-04 13:50:20
【问题描述】:
我已尝试对我的问题进行研究,但未能解决它。我正在尝试点击 Ajax POST。我读过最流行的问题是由于 csrf_token,但我相信我处理得当?
我不断收到此错误:
POST http://example.com/refreshCalendar 500 (Internal Server Error)
这是我的代码...
我的 master.blade.php 文件顶部的 csrf 令牌的元标记
<meta name="token" content="{{ csrf_token() }}">
路线:
Route::post('/refreshCalendar', ['as' => 'refreshCalendar', 'uses' =>'Calendar@refreshCalendar']);
js函数
function refreshCalendar(obj){
var month = obj.data('month');
var year = obj.data('year');
history.pushState(null, null, '/month/'+month+'/year/'+year);
var data = {
"month":month,
"year":year,
_token:$('meta[name="csrf-token"]').attr('content')
};
$.ajax({
type: "POST",
url: '/refreshCalendar',
dataType: 'html',
async:true,
data: data,
success: function(data){
$('#calendarHolder').html(data);
},
error: function(){alert("There was an error retrieving information");return false;}
});
}
我的控制器:
namespace App\Http\Controllers;
use DateTime;
use Illuminate\Http\Request;
class Calendar extends Controller
{
public function refreshCalendar(Request $request)
{
//Set data to $request
$data = $request->all();
return show($data['month'], $data['year'], true);
}
}
【问题讨论】:
-
失败请求的响应正文是什么?使用您的浏览器网络检查器来读取错误。
-
你说的是console.log中的内容吗?它提醒我“检索信息时出错”,但 console.log 显示“POST example.com/refreshCalendar 500(内部服务器错误)”
-
使用浏览器的调试器查看真正发送到服务器的内容。它可能包含线索。尝试关闭此路由的 CSRF 检查,看看会发生什么。
-
尝试在
/storage/log/laravel.log中查找 Laravel 错误并在此处发布最新的错误。我最好的猜测是你的 show() 方法没有被定义 -
我注释掉了 // \App\Http\Middleware\VerifyCsrfToken::class,并且它工作正常......我猜它仍然与令牌有关?