0
votes

Click on submit button gives Status Code: 405 Method Not Allowed

message: "The POST method is not supported for this route. Supported methods: GET, HEAD."

exception: "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"

file: "C:\laragon\www\steklo-group\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php"

but I don't have something special in my form. What is wrong? I have another form done in the same way and working.

<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');

in controller I just setup basic actions to check it works

    public function add_to_cart(Request $request) {

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

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

    }
1
method="POST" and Route::get(...). What do you think?Levente Otta
Nice. So stupid mistake. Thanksschel4ok

1 Answers

0
votes

Answer is very easy method="POST" and Route::get(...) Thanks a lot, Levente!