[英]Laravel Ajax 419 Error on Production but not Local Development and Nothing In /storage/logs/laravel.log
因此,我遇到了這個問題,在通過項目提交AJAX請求時出現419錯誤代碼。 我知道此錯誤是由於CSRF令牌未傳遞或無效引起的。
背后的故事:我在項目上創建了一個“維護模式”。 此維護模式通過顯示503錯誤頁面來限制對前端的訪問,但仍允許我的管理員訪問后端以更新站點等。這是使用某些中間件完成的。 有關我如何完成此功能的更多信息,請參見我的github頁面上的代碼。
https://github.com/JDsWebService/ModelAWiki/commit/263a59ebba42688d4a232a5838334b9ee419504c
那么,這可能是我的生產環境中的503錯誤頁面存在問題嗎? 我不太確定
我已經看過關於SOF的問答,但似乎並沒有幫助我。
這是生產站點,請查看控制台以獲取更多信息: http : //modelawiki.com/
這是與419錯誤有關的代碼:
使用Javascript
$(document).ready(function() {
// CSRF Ajax Token
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Add Section Post Button
$('#subscribeButton').click(function(event) {
/* Act on the event */
// Clear Feedback Boxes
$('#valid-feedback').css("display", "none");
$('#invalid-feedback').css("display", "none");
// Input Field
var email = $('#email').val();
var token = $('meta[name="csrf-token"]').attr('content');
console.log(email);
console.log(token);
// Post
$.ajax({
type: 'POST',
url: '/email/subscribe/',
dataType: 'json',
data: {
email: email,
"_token": token,
},
success: function (data) {
// Check Server Side validation
if($.isEmptyObject(data.errors)){
// Show the Feedback Div
$('#valid-feedback').css("display", "block");
// Add the Bootsrapt Is Invalid Class
$('#email').addClass('is-valid');
// Validation Failed Display Error Message
$('#valid-feedback').text(data['success']);
// Animate the Object
$('#email').animateCss('tada', function() {});
console.log(data['success']);
}else{
// Show the Feedback Div
$('#invalid-feedback').css("display", "block");
// Add the Bootsrapt Is Invalid Class
$('#email').addClass('is-invalid');
// Validation Failed Display Error Message
$('#invalid-feedback').text(data.errors[0]);
// Animate the Object
$('#email').animateCss('shake', function() {});
console.log(data.errors);
}
},
error: function (data) {
console.log(data);
}
}); // End Ajax POST function
}); // End Click Event
// On Focus of the Email Box
$('#email').focus(function(event) {
/* Act on the event */
$('#valid-feedback').css("display", "none");
$('#invalid-feedback').css("display", "none");
});
}); // End Document Ready
HTML表格
<div class="input-group input-group-newsletter">
<input type="email" class="form-control" placeholder="Enter email..." aria-label="Enter email..." aria-describedby="basic-addon" id="email">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" id="subscribeButton">Notify Me!</button>
</div>
<div id="invalid-feedback" class="invalid-feedback"></div>
<div id="valid-feedback" class="valid-feedback"></div>
</div>
標頭 (這表明CSRF令牌實際上位於503錯誤頁面上)
<meta name="csrf-token" content="{{ csrf_token() }}">
同樣,此代碼在我的本地環境中有效,但在我的生產環境中無效。 (我也知道AJAX請求可以並且正在生產環境中在我網站的其他部分中很好地處理,所以我知道這不是服務器問題,並且與代碼有關)
以防萬一這也是我的控制器代碼。
// Store the Email into the database
public function subscribe(Request $request) {
// Validate the request
$validator = Validator::make($request->all(), [
'email' => 'required|email|max:255|unique:email_subscribers,email',
]);
// If the validation fails
if($validator->fails()) {
return response()->json([
'errors' => $validator->errors()->all(),
]);
}
// New Section Object
$subscription = new EmailSubscription;
// Add Name into Section Object
$subscription->email = $request->email;
// Save the Section
$subscription->save();
// Return The Request
return response()->json([
'success' => 'You have successfully subscribed! Check your email!'
]);
}
而我的路線
// Email Routes
Route::prefix('email')->group(function() {
// Create Post Route for subscribing
Route::post('/subscribe', 'EmailSubscriptionsController@subscribe')->name('email.subscribe');
});
萬一它可以幫助某人,我們也會遇到相同的問題,這是由於服務器空間磁盤問題所致。 因此,Laravel無法寫入文件(特別是sessions.php)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.