Laravel Symfony\Component\HttpKernel\Exception\HttpException-Ajax post请求
在多步骤表单的步骤2中,如果选择单选按钮并单击“转到步骤3”,或者如果未选择单选按钮并单击“转到步骤3”,则始终会出现以下错误:Laravel Symfony\Component\HttpKernel\Exception\HttpException-Ajax post请求,laravel,Laravel,在多步骤表单的步骤2中,如果选择单选按钮并单击“转到步骤3”,或者如果未选择单选按钮并单击“转到步骤3”,则始终会出现以下错误: {message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…} exception"Symfony\Component\HttpKernel\Exception\HttpException" file "/Users/johnw/projects/store/vend
{message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
exception"Symfony\Component\HttpKernel\Exception\HttpException"
file "/Users/johnw/projects/store/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php" line : 203 message: "" trace:[{,…},…]
你知道问题出在哪里吗
步骤2的Html:
<form method="post" id="step2form" action="">
<h6>Payment method</h6>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="radio" name="payment_method" value="option1">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Payment method 1</span>
</label>
</div>
<br>
<div class="form-check">
<input class="form-check-input" type="radio" name="payment_method" value="option1">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Payment method 2</span>
</label>
</div>
</div>
<br>
<div class="text-right">
<button type="button" href="#step3" data-toggle="tab" role="tab"
class="btn btn-outline-primary prev-step">
Go back to step 2
</button>
<input type="submit" href="#step2" id="goToStep3"
class="btn btn-primary btn float-right next-step"
value="Go to step 3"/>
</div>
</form>
阿贾克斯:
$('goToStep3')。在('click',函数(事件){
event.preventDefault();
var custom_form=$(“#”+页面_form_id);
$.ajax({
方法:“张贴”,
url:{{route('products.storePaymentMethods',compact('id','slug'))}},
数据:自定义表单.serialize(),
数据类型:“json”,
成功:函数(数据、文本状态、jqXHR){
},
错误:函数(数据){
var errors=data.responseJSON;
var errorsHtml='';
$.each(错误['errors'],函数(索引,值){
errorsHtml+='- '+value+'
;
console.log(errorsHtml);
});
$('#response').show().html(errorsHtml);
}
});
您尚未在表单中添加任何csrf令牌。有几种方法可以做到这一点,如下面提到的方法-
- 在表单中添加
{{csrf_field()}
- 或者在ajax请求中添加令牌,比如
\u令牌:{{{csrf\u token()}}
public function storePaymentMethods(Request $request, $id, $slug = null, Validator $validator){
$validator = Validator::make($request->all(),[
'payment_method' => 'required',
]);
if($validator->passes())
{
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
$errors = $validator->errors();
$errors = json_decode($errors);
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
$('#goToStep3').on('click', function (event) {
event.preventDefault();
var custom_form = $("#" + page_form_id);
$.ajax({
method: "POST",
url: '{{ route('products.storePaymentMethods', compact('id','slug') ) }}',
data: custom_form.serialize(),
datatype: 'json',
success: function (data, textStatus, jqXHR) {
},
error: function (data) {
var errors = data.responseJSON;
var errorsHtml = '';
$.each(errors['errors'], function (index, value) {
errorsHtml += '<ul class="alert alert-danger mt-3"><li class="text-danger">' + value + '</li></ul>';
console.log(errorsHtml);
});
$('#response').show().html(errorsHtml);
}
});