如何修复 Laravel 5.8 Ajax 表单提交中的内部服务器错误
Posted
技术标签:
【中文标题】如何修复 Laravel 5.8 Ajax 表单提交中的内部服务器错误【英文标题】:How to fix Internal Server Error in Laravel 5.8 Ajax Form Submit 【发布时间】:2020-01-08 20:54:34 【问题描述】:我正在使用 Ajax 在 Laravel 中提交表单,但它挂起,检查 console.log 我得到内部服务器错误 500
我使用的是 Laravel 5.8,csrf 令牌包含在元名称和 ajax 函数中,使用 console.log 包括 _token 在内的所有值都出现在控制台中
这是表单控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response;
use App\Contact;
class FormController extends Controller
public function index()
return view('ajax-form');
public function store(Request $request)
request()->validate([
'name' => 'required',
'email' => 'required|email|unique:users',
'mobile_number' => 'required|unique:users'
]);
$data = $request->all();
$check = Contact::insert($data);
$arr = array('msg' => 'Something goes to wrong. Please try again lator', 'status' => false);
if($check)
$arr = array('msg' => 'Successfully submit form using ajax', 'status' => true);
return Response()->json($arr);
这是我的刀片文件
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="csrf-token" content=" csrf_token() ">
<title>Laravel 5.7 </title>
<link rel="stylesheet" href="asset('css/bootstrap.min.css')" />
<script src=" asset('js/jquery.js') "></script>
<script src=" asset('js/jquery.validate.js') "></script>
<script src=" asset('js/additional-methods.min.js') "></script>
<style>
.error color:red;
</style>
</head>
<body>
<div class="container">
<h2 style="margin-top: 10px;">Laravel 5.7 Ajax Form Submission Example - <a href="https://www.tutsmake.com" target="_blank">TutsMake</a></h2>
<br>
<br>
-- <form id="contact_us" method="post" action=" route('name') "> --
<form id="contact_us" method="post" action="javascript:void(0)">
@csrf
<div class="form-group">
<label for="formGroupExampleInput">Name</label>
<input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
<span class="text-danger"> $errors->first('name') </span>
</div>
<div class="form-group">
<label for="email">Email Id</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
<span class="text-danger"> $errors->first('email') </span>
</div>
<div class="form-group">
<label for="mobile_number">Mobile Number</label>
<input type="text" name="mobile_number" class="form-control" id="mobile_number" placeholder="Please enter mobile number" maxlength="10">
<span class="text-danger"> $errors->first('mobile_number') </span>
</div>
<div class="alert alert-success d-none" id="msg_div">
<span id="res_message"></span>
</div>
<div class="form-group">
<button type="submit" id="send_form" class="btn btn-success">Submit</button>
</div>
</form>
</div>
<script>
if ($("#contact_us").length > 0)
$("#contact_us").validate(
rules:
name:
required: true,
maxlength: 50
,
mobile_number:
required: true,
digits:true,
minlength: 10,
maxlength:12,
,
email:
required: true,
maxlength: 50,
email: true,
,
,
messages:
name:
required: "Please enter name",
maxlength: "Your last name maxlength should be 50 characters long."
,
mobile_number:
required: "Please enter contact number",
minlength: "The contact number should be 10 digits",
digits: "Please enter only numbers",
maxlength: "The contact number should be 12 digits",
,
email:
required: "Please enter valid email",
email: "Please enter valid email",
maxlength: "The email name should less than or equal to 50 characters",
,
,
submitHandler: function(form)
$.ajaxSetup(
headers:
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
);
$('#send_form').html('Sending..');
console.log($('#contact_us').serialize());
$.ajax(
url: " url('save-form')",
type: "POST",
data: $('#contact_us').serialize(),
success: function( response )
$('#send_form').html('Submit');
$('#res_message').show();
$('#res_message').html(response.msg);
$('#msg_div').removeClass('d-none');
document.getElementById("contact_us").reset();
setTimeout(function()
$('#res_message').hide();
$('#msg_div').hide();
,1000);
);
)
</script>
</body>
</html>
我的路线
Route::post('save-form', 'FormController@store');
我需要将表单提交到数据库。
【问题讨论】:
status 500此错误响应是通用的“包罗万象”响应。你必须查看storage/logs/laravel.log
才能知道发生了什么
【参考方案1】:
这是什么use Validator,Redirect,Response;
?
使用 laravel 辅助函数:response()->json($arr);
或只是 return $arr;
【讨论】:
感谢@Alexis Navarro,我已将验证器、重定向和响应放在不同的行上。我使用 response()->json($arr);在我的控制器中,我仍然遇到同样的错误, 去掉那些use语句,它们有什么作用?使用 laravel 辅助函数或阅读有关外观的文档,因为它们不是正确的命名空间。以上是关于如何修复 Laravel 5.8 Ajax 表单提交中的内部服务器错误的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 5.8 文件上传使用 PUT 方法和 ajax
使用 AJAX 和 Laravel 5.8 的动态依赖选择选项