我正在尝试使用jquery验证我的表单。提交时,Laravel在web.php中找不到路由。当我以前仅用model :: Form进行操作时,它就起作用了。做了很多尝试,但没有一个给我解决方案
编辑:419执行消失了:现在面临500个内部错误
web.php:
Route::post('/profile/sendmail', 'VendorController@send_mail')->name('profile.sendmail');
VendorController:
public function send_mail(Request $request){
dd("test");
}
contact.blade.php
<div class="events single-event">
<div class="o-grid">
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
<h4>@lang('profile.contactTitle')</h4>
</div>
<div class="o-grid__col u-12/12@sm" id="alert_modal" style="margin-top: 150px; display: none">
<div class="modal-body">
<h3>Alert</h3>
<h5 id="alert_message"></h5>
</div>
<input type="hidden" id="profile_id" value="{{ $profile->id }}">
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('', __('profile.contactSalutation').'*') !!}
@if( Session::get('urlLang') == "en" )
{!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
@else
{!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
@endif
</div>
<br>
</div>
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
<p style="color: #696978; font-size: 14px; text-align: right">@lang('profile.mandatoryField')</p>
</div>
</div>
</div>
<div class="o-grid">
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_first_name', __('profile.contactFirstName').'*') !!}
{!! Form::text('contact_first_name', null, ['placeholder' => __('profile.contactFirstName'),'class' => 'c-input required','id' => 'contact_first_name','required']) !!}
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_e_mail', __('profile.contactEmail').'*') !!}
{!! Form::text('contact_e_mail', null, ['placeholder' => __('profile.contactEmail'),'class' => 'c-input required email','id' => 'contact_e_mail','required']) !!}
<span class="text-danger">{{ $errors->first('contact_e_mail') }}</span>
</div>
</div>
<div class="o-grid__col u-6/12">
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_last_name', __('profile.contactLastName').'*') !!}
{!! Form::text('contact_last_name', null, ['placeholder' => __('profile.contactLastName'),'class' => 'c-input required','id' => 'contact_last_name','required']) !!}
</div>
<div class="o-grid__col u-12/12@sm">
{!! Form::label('contact_phone', __('profile.contactPhone')) !!}
{!! Form::text('contact_phone', null, ['placeholder' => __('profile.contactPhone'),'class' => 'c-input','id' => 'contact_phone']) !!}
</div>
</div>
<div class="o-grid__col u-12/12">
<div class="o-grid__col">
{!! Form::label('text', __('profile.contactMessageInfo')) !!}
{!! Form::textarea('contact_text',null,['class' => 'c-input c-input__text required','placeholder' => __('profile.contactMessageInfo'),'id' => 'contact_text','required']) !!}
</div>
</div>
<div class="o-grid__col u-12/12">
<input class="c-input__checkbox required" id="toc" type="checkbox" name="agree_to_toc" value="1" required>
<label class="c-input__checkboxLabel p2" for="toc">
{!! @trans('global.formTacInfo', [
'class' => 'c-link c-link__primary',
'link_datenschutz' => route('static.show', ['folder_id' => 28, 'slug' => "datenschutz"])
]) !!}</label>
</div>
<div class="o-grid__col u-text-right">
<button id="submit_contact_form" class="c-btn c-btn--small c-btn--red" type="submit" style="display: none" onclick="submitForm()">
<span>@lang('profile.contactSendMessage')</span></button>
</div>
</div>
</div>
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#toc').change(function() {
if($(this).is(":checked")) {
$('#submit_contact_form').show();
}
else{
$('#submit_contact_form').hide();
}
})
function submitForm() {
var profile_id = $("#profile_id").val();
$.ajax({
method: 'POST',
url: '{{route("profile.sendmail")}}',
data: {profile_id: profile_id},
dataType: "json",
success: function(data){
$('#alert_message').text(data.message);
$('#alert_modal').show();
},
error: function(data){
$('#alert_message').text(data.message);
$('#alert_modal').show();
}
});
}
</script>
答案 0 :(得分:2)
在发布路线上,您无需在URL中传递变量:
将路线更改为:
Route::post('/profile/sendmail', 'VendorController@send_mail')->name('profile.sendmail');
和ajax请求:
$.ajax({
method: 'POST',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '{{route("profile.sendmail")}}',
data: {profile_id_var: profile_id_var},
在您的控制器上:
$request->get('profile_id_var');
答案 1 :(得分:2)
419状态通常与令牌问题有关。
尝试更改'_token'
对此:
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
答案 2 :(得分:0)
我认为您的行data: profile_id_var
不正确,因为您使用profile_id
参数(而不是profile_id_var
)定义了路线。
如果您尝试data: { profile_id: profile_id_var }
怎么办?