What is $book on that page ? The id of a book?
Did you mean to do
axios.post('/books/{{$book->id}}/comments', {
I tried to submit the form using axios to avoid refresh the page but it fails with post method
<form
action="/books/{{$book->title}}/comments" method="POST"
>
@csrf
<header class="flex items-center">
<img
src="https://i.pravatar.cc/50?u={{auth()->id()}}"
width="50" height="50" alt=""
class="rounded-full "
>
<h2 class="ml-4 font-semibold">Want to participate?</h2>
</header>
<div class="mt-4">
<textarea
class="w-full p-2 text-sm focus:outline-none focus:ring"
name="body"
rows="5"
placeholder="Quick,think of something to say!"
></textarea>
<x-form.errors name="body"/>
</div>
<div class="flex justify-end mt-6 pt-6 border-t border-gray-200 ">
<x-form.button id="submit-button">Publish</x-form.button>
</div>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.27.2/axios.min.js"></script>
<script>
document.getElementById('submit-button').addEventListener("click", function(event){
event.preventDefault()
axios.post('/books/{{$book}}/comments', {
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
})
</script>
i get this error 404 (Not Found) !!
@mutaz you expect body, but don't send any data at all. Axios does not automatically grab your form data. You need to pass it to it (the {} part)
Please or to participate in this conversation.