【发布时间】:2020-05-24 09:04:33
【问题描述】:
我无法创建新的数据库记录,因为提交后它总是显示 POST 405(不允许)。我试图将 laravel 路由从 api.php 移动到 web.php 但它没有用。还尝试修改 axios.post 链接(create、/create、localhost://Reservationsystem/public/create),但它也没有给我结果。
Web.php
Route::resource('/reservation','ReservationController');
Route::get('/{any}', 'SinglePageController@index')->where('any', '.*');
Routes.js
import Vue from 'vue'
import Router from 'vue-router'
import Home from './components/HomeComponent.vue'
import Reservations from './components/ReservationsComponent.vue'
Vue.use(Router);
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/reservations',
name: 'reservations',
component: Reservations
}
]
})
HomeComponent.vue
<script>
import axios from 'axios';
export default {
data: function () {
return {
reservation: {
name: '',
surname: '',
email: '',
date: '',
place: ''
}
}
},
methods: {
addReservation: function () {
let uri = 'reservation';
axios.post(uri, this.reservation).then(res => this.reservation = [...this.reservation, res.data]);
}
}
}
</script>
控制器方法:
public function store(Request $request)
{
$reservation= new Reservation($request->all());
$reservation->save();
return new ReservationResource($reservation);
}
【问题讨论】:
-
请在控制器中包含代码。你有
public function store吗? -
是的。现在添加方法
-
尝试将路由
Route::resource('/reservation','ReservationController');留在api.php中,然后将axios 调用到/api/reservationurl。 -
拨打电话时请检查您的网络标签。呼叫的完整网址是什么(在网络选项卡中)?
-
当我尝试访问 post 方法 url 时,它显示空白页,这很奇怪,经过一些研究,我发现在 url 中公开后,我需要添加 index.php (localhost/project/public/ index.php/...) 好像是服务器配置问题..
标签: php laravel vue.js post axios