发布请求状态码:405方法不允许

时间:2020-01-12 20:56:38

标签: laravel vue.js post axios

单击“提交”按钮将给出状态码:405方法不允许

消息:“此路由不支持POST方法。受支持 方法:GET,HEAD。”

例外: “ Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException”

文件: “ C:\ laragon \ www \ steklo-group \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php”

但是我的表格中没有特殊内容。怎么了? 我还有另一种以相同的方式工作的表格。

<template>

  <div class="cart">
    <form method="POST" id="add_to_cart" action="/add_to_cart" @submit.prevent="onSubmit">
      <input type="hidden" name="_token" :value="csrf">
      <div class="quantity">
        <button type="button" class="minus_quantity" v-on:click="minus_quantity" v-long-press="300" @long-press-start="longMinusStart" @long-press-stop="longMinusStop">-</button>
        <input type="number" class="input-text qty text" step="1" min="1" max="9999" name="quantity" value="1" title="Кол-во" v-model.number="quantity">
        <button type="button" class="plus_quantity" v-on:click="plus_quantity" v-long-press="300" @long-press-start="longPlusStart" @long-press-stop="longPlusStop">+</button>
      </div>
      <button type="submit" name="add-to-cart" class="button button-cart">В корзину</button>
    </form>   
  </div>

</template>

<script>
    import LongPress from 'vue-directive-long-press';

    export default {

        name: "addtocart",

        data: function () {
          return {
            csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
            quantity: 1,
            plusInterval: null,
            minusInterval: null,
            success: false,
            errors: []
          }
        },
        directives: {
          'long-press': LongPress,
        },
        methods: {
          onSubmit() {
            axios.post('/add_to_cart', this.$data)
            .then(this.onSuccess)
            .catch(error => this.errors = error.response.data);
          },
          onSuccess(response) {
            this.success = true;
            this.$emit('added_to_cart', response);
          },

        },
    }
</script>
Route::get('/add_to_cart', 'Controller@add_to_cart');

在控制器中,我只是设置基本操作以检查其是否有效

    public function add_to_cart(Request $request) {

        request()->validate([
            'quantity' => 'numeric'
        ]);

        return [
            'message' => 'Товар добавлен в корзину.', 
            'alertclass' => '', // 'success', 'info', 'error'
                ];

    }

1 个答案:

答案 0 :(得分:0)

答案很简单self.set_table_name = animals_ownermethod="POST" 非常感谢,Levente!