Javascript 422(使用ajax提交表单时出现不可处理的实体错误
我试图使用模式提交表单,但收到了此错误。422(不可处理实体)。在我的菜单模型中,我使用受保护的$table='Menu'指定了我的表名$MenuJavascript 422(使用ajax提交表单时出现不可处理的实体错误,javascript,php,ajax,laravel,Javascript,Php,Ajax,Laravel,我试图使用模式提交表单,但收到了此错误。422(不可处理实体)。在我的菜单模型中,我使用受保护的$table='Menu'指定了我的表名$Menu <script type="text/javascript"> $(document).ready(function(){ $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); 我的方法
<script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
我的方法
public function post(Request $req)
{
if($req->ajax()){
$req->validate([
'Item_Code'=>'required',
'Name'=>'required',
'Printer'=>'required',
'Category'=>'required',
'Price'=>'required',
'Stocks'=>'required',
'Image'=>'required'
]);
$post = new Menu;
$post->Item_Code = $req->Item_Code;
$post->Name = $req->Name;
$post->Printer = $req->Printer;
$post->Category = $req->Category;
$post->Price = $req->Price;
$post->Stocks = $req->Stocks;
$post->Image = $req->Image;
$post->save();
return response()->json();
}
}
我的路线
Route::post('/post', 'AdminController@post')->name('create.inventory');
我的情态
<div class="modal-body">
<form>
<label for="required-input" class="require">Item Code:</label>
<input type = "text" class="form-control" placeholder="Item Code" id = "Item_Code">
<label for="placeholder-input" class="require">Name:</label>
<input type= "text" class="form-control" placeholder="Name" id = "Name">
<label for="single-select" class="require">Printer</label>
<select id="Printer" class="form-control">
<option>Kitchen</option>
<option>Bar</option>
</select>
<label for="single-select">Category</label>
<select id="Category" class="form-control">
<option>Japanese</option>
<option>Beverage</option>
</select>
<label for="required-input" class="require">Input Price:</label>
<input type ="number" class="form-control" placeholder="Price" id="Price">
<label for="required-input" class="require">Quantity:</label>
<input type ="number" class="form-control" placeholder="Quantity" id="Stocks">
<label for="required-input" class="require">Image:</label>
<input type = "file" class="form-control" id="Image">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
项目代码:
姓名:
打印机
厨房
酒吧
类别
日本人
饮料
投入价格:
数量:
图片:
接近
提交
422是Laravel验证的状态代码
检查输入数据,可能其中一个值为null这就是我如何创建这样一个函数的方法 引导模型-我在表单中添加了一个ID,还添加了一个div,其中包含用于验证和成功消息的消息类
<div class="modal-body">
<div class="messages"></div>
<form id="productForm">
{{ csrf_field() }}
<label for="required-input" class="require">Item Code:</label>
<input type = "text" class="form-control" placeholder="Item Code" id = "Item_Code">
<label for="placeholder-input" class="require">Name:</label>
<input type= "text" class="form-control" placeholder="Name" id = "Name">
<label for="single-select" class="require">Printer</label>
<select id="Printer" class="form-control">
<option>Kitchen</option>
<option>Bar</option>
</select>
<label for="single-select">Category</label>
<select id="Category" class="form-control">
<option>Japanese</option>
<option>Beverage</option>
</select>
<label for="required-input" class="require">Input Price:</label>
<input type ="number" class="form-control" placeholder="Price" id="Price">
<label for="required-input" class="require">Quantity:</label>
<input type ="number" class="form-control" placeholder="Quantity" id="Stocks">
<label for="required-input" class="require">Image:</label>
<input type = "file" class="form-control" id="Image">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
你能给我个建议吗example@FrancisLobosApeajax请求中的一些数据值为null,在控制器中使用
validate()进行验证
用于检查所有需要的字段的函数检查我自己。422是Laravel验证的状态代码
是我的FormRequest
类型参数的一个键。我从一开始就走错了方向。投票结果是:你使用id=…
作为输入,但你应该使用name=
(代替或添加id)以在laravel中正确获取它们。422表示您的验证失败,这是因为您的请求为空。您还需要帮助吗?@Yosef是的,我仍然需要help@FrancisLobosApe查看我发布的解决方案:)
<div class="modal-body">
<form>
<label for="required-input" class="require">Item Code:</label>
<input type = "text" class="form-control" placeholder="Item Code" id = "Item_Code">
<label for="placeholder-input" class="require">Name:</label>
<input type= "text" class="form-control" placeholder="Name" id = "Name">
<label for="single-select" class="require">Printer</label>
<select id="Printer" class="form-control">
<option>Kitchen</option>
<option>Bar</option>
</select>
<label for="single-select">Category</label>
<select id="Category" class="form-control">
<option>Japanese</option>
<option>Beverage</option>
</select>
<label for="required-input" class="require">Input Price:</label>
<input type ="number" class="form-control" placeholder="Price" id="Price">
<label for="required-input" class="require">Quantity:</label>
<input type ="number" class="form-control" placeholder="Quantity" id="Stocks">
<label for="required-input" class="require">Image:</label>
<input type = "file" class="form-control" id="Image">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<div class="modal-body">
<div class="messages"></div>
<form id="productForm">
{{ csrf_field() }}
<label for="required-input" class="require">Item Code:</label>
<input type = "text" class="form-control" placeholder="Item Code" id = "Item_Code">
<label for="placeholder-input" class="require">Name:</label>
<input type= "text" class="form-control" placeholder="Name" id = "Name">
<label for="single-select" class="require">Printer</label>
<select id="Printer" class="form-control">
<option>Kitchen</option>
<option>Bar</option>
</select>
<label for="single-select">Category</label>
<select id="Category" class="form-control">
<option>Japanese</option>
<option>Beverage</option>
</select>
<label for="required-input" class="require">Input Price:</label>
<input type ="number" class="form-control" placeholder="Price" id="Price">
<label for="required-input" class="require">Quantity:</label>
<input type ="number" class="form-control" placeholder="Quantity" id="Stocks">
<label for="required-input" class="require">Image:</label>
<input type = "file" class="form-control" id="Image">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<script>
var form = $('#productForm');
var formData = form.serialize();
var createUrl = '{{ route('create.inventory') }}';
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: createUrl,
type: 'post',
data: formData,
dataType: 'json',
success: function (response) {
var successHtml = '<div class="alert alert-success">'+
'<button type="button" class="close" data-dismiss="alert">×</button>'+
'<strong><i class="glyphicon glyphicon-ok-sign push-5-r"></i></strong> '+ response.message +
'</div>';
var messages = $('.messages');
$(messages).html(successHtml);
window.setTimeout(function() {
location.reload();
}, 800);
},
error: function(response) {
var errors = response.responseJSON.errors;
var errorsHtml = '<div class="alert alert-danger"><ul>';
$.each( errors, function( key, value ) {
errorsHtml += '<li>'+ value[0] + '</li>';
});
errorsHtml += '</ul></div';
$('.messages').html(errorsHtml);
}
});
});
</script>
public function post(Request $request)
{
if ($request->ajax()) {
$validator = Validator::make($request->all(), [
'Item_code' => 'required',
'Name' => 'required',
'Printer' => 'required',
'Category' => 'required',
'Price' => 'required',
'Stocks' => 'required',
'Image' => 'required',
]);
if ($validator->fails()) {
return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
} else {
$post = new Menu([
'Item_name' => $request->input('Item_code'),
'Name' => $request->input('Name'),
'Printer' => $request->input('Printer'),
'Category' => $request->input('Category'),
'Price' => $request->input('Price'),
'Stocks' => $request->input('Stocks'),
'Images' => $request->input('Images')
]);
$post->save();
return response()->json(['success' => true, 'message' => 'success'], 200);
}
}
}