我正在使用laravel5.4。当使用ajax提交表单时,我得到MethodNotAllowedHttpException。表单方法是post,url在web routes文件中定义。如何解决此错误。
web.php
Route::post('/submit-enquiry','HomeController@submitEnquiry');
register.blade.php
$("#btn-submit").on('click',function(){
$.ajax({
type:'post',
url: "submit-enquiry",
data: $('#enquiry-form').serialize()
}).done(function(result){
// if($.isEmptyObject(result.error)){
// alert(result.success);
// }else{
// printErrorMsg(result.error);
// }
});
});
Controller
public function submitEnquiry(Request $request){
try{
$params = $request->all();
$validator = Validator::make($params, [
'name' => 'required|max:200',
'email' => 'required|email|max:200',
'phone' => 'required|max:20',
'date_of_departure' => 'required|date_format:Y-m-d',
'return_date' => 'required|date_format:Y-m-d'
]);
if($validator->fails()){
return response()->json(['error' => $validator->errors()->all()]);
}
} catch (\Exception $ex) {
return response()->json(['error' => $ex->getMessage()]);
}
}