laravel - Laravel 中的 Vue 表单组件在提交时收到 422 错误
问题描述
我正在尝试将一个 Vue 表单组件添加到我的 Laravel 应用程序中,这样我就可以在整个应用程序的几个地方重用它。但是当我提交表单时,我收到一个 422 错误,说找不到路由。
表单组件:
<template>
<form @submit.prevent="mail" method="POST">
</form>
</template>
<script>
import FormMixin from '../FormMixin';
export default {
mixins: [ FormMixin ],
data() {
return {
'action': 'submit',
}
}
}
</script>
表单混合
export default {
data() {
return {
fields: {},
errors: {},
success: false,
loaded: true,
action: '',
}
},
methods: {
mail() {
if (this.loaded) {
this.loaded = false;
this.success = false;
this.errors = {};
axios.post(this.action, this.fields).then(response => {
this.fields = {}; //Clear input fields.
this.loaded = true;
this.success = true;
}).catch(error => {
this.loaded = true;
if (error.response.status === 422) {
this.errors = error.response.data.errors || {};
}
});
}
},
},
}
控制器
public function mail(NewConctactRequest $contact) {
Mail::to('example@example.com')->send(new NewContact($contact));
return redirect()->route('thank you');
return response()->json(null, 200);
}
网络路由
Route::get('/home', 'HomeController@index')->name('home');
Route::get('adventures', 'PageController@adventures')->name('adventures');
Route::get('crew', 'PageController@crew')->name('crew');
Route::get('events', 'PageController@events')->name('events');
Route::get('gallery', 'PageController@gallery')->name('gallery');
Route::get('thank_you', 'PageController@thank_you')->name('thank you');
Route::get('contact', 'ContactController@show')->name('contact');
Route::post('submit', 'ContactController@mail')->name('mail contact');
我已经安装了 Axios,并且 CSRF 令牌设置在文档的头部。当我将表单用作标准表单(不使用 Vue)时,它会正确提交。
解决方案
推荐阅读
- python - 在进行糖尿病视网膜病变项目时遇到此错误 - 索引 0 超出轴 0 大小为 0 的范围
- java - Spring,Hibernate:尝试删除时“与会话无关的集合所有者”
- java - 从 AlertDialog 保存不同的值
- python - 如何在先验算法中将某些列设置为前件和后件?
- git - Mingw-w64:ssh-add 一直有效,直到 git fetch(连接到代理时出错:文件描述符错误)
- python - 在 Python 中将外键表中的数据收集到 CSV 中
- vue.js - Vue汇总构建的min.js文件不起作用
- python - 将 CountVectorizer 应用于整数列表,V2
- python - 从另一个类的输入变量修改实例的值
- php - How to show array data form controller in blade file (html table)? Laravel