【问题标题】:axios post 405 not allowed | Laravel + vue.js不允许 axios 发布 405 | Laravel + vue.js
【发布时间】: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/reservation url。
  • 拨打电话时请检查您的网络标签。呼叫的完整网址是什么(在网络选项卡中)?
  • 当我尝试访问 post 方法 url 时,它显示空白页,这很奇怪,经过一些研究,我发现在 url 中公开后,我需要添加 index.php (localhost/project/public/ index.php/...) 好像是服务器配置问题..

标签: php laravel vue.js post axios


【解决方案1】:

你可能得到 405 的原因是因为路由不是 Post 与 addReservation 方法中的 post 重合。我将使用以下 post 路由:

Route::post('/reservation/store','ReservationController@store');

对于 axios 中的 uri,我会使用:

let uri = 'reservation/store';

这是一篇文章的链接,展示了我如何在 Laravel 中使用 axios。 Blog Post With Code

【讨论】:

    【解决方案2】:

    你使用 https 吗?如果是,请不要在地址末尾使用/

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2020-07-25
      • 2021-12-12
      • 2023-02-17
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2015-08-25
      相关资源
      最近更新 更多