SmokeTM
1 year ago
79
12
Laravel

Ajax request throws methodnotallowed exception

Posted 1 year ago by SmokeTM

Hi guys as the title says my ajax request throws a methodnotallowed excption. I dont know what's wrong with my code maybe you guys can help me.

@forelse ($articles as $article) <!--/article/vote/{{ $article->id }}-->
            <form method="post" action="">
              @csrf
              <button class="btn vote {{ Auth::check() && Auth::user()->votedFor($article) ? 'btn-success' : 'btn-default' }}">{{ $article->votes->count() }}</button>
            </form>
            <script>
            $(document).on('click','.vote',function(){
              var token = $('meta[name="csrf-token"]').attr('content');
              $.ajax({

                  type:'POST',
                  url:"{!! URL::to('article/vote) !!}/{!! $article->id !!}",
                  dataType: 'JSON',
                  data: {
                      "_method": 'POST',
                      "_token": token,
                  },
                  success:function(data){
                      console.log('success');
                      console.log(data);
                  },
                  error:function(){

                  },
              });
            });
            </script>
            <div class="headline">
                <h1 class="title"><a href="{{ $article->link }}">{{ $article->title }}</a></h1>
                <a href="/article/categorie/{{ $article->categorie->slug }}" class="badge badge-secondary" style="background: {{ $article->categorie->color }}">{{ $article->categorie->title }}</a>
                <p class="meta text-muted">
                  <span>{{ $article->updated_at->diffForHumans() }} by {{ $article->user->name}} </span> &middot;
                  <a class="text-muted" href="{{ url('/article', $article->id) }}">{{count($article->comments)}} Comments</a>
                </p>
            </div>
            @empty
            <h2>There are no entries, sorry!</h2>
            @endforelse
Controller

public function store(Article $article)
    {
        if(Request::ajax()) {
            return test;
        }

        $user = auth()->user();

        if($user->votedFor($article)) {
          $user->unvoteFor($article);
        } else {
          $user->voteFor($article);
        }

        return back();
    }

Please sign in or create an account to participate in this conversation.