Bolboa Bolboa - 18 days ago 6
React JSX Question

Fetch request failing (laravel)

I am using the following

fetch
to make a
POST
request...

componentDidMount(){
fetch('http://laravel.dev/content', {
method: 'POST',
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({

})
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
});
}


But the request is failing. I get the following error...

POST http://laravel.dev/content 500 (Internal Server Error)


My
route
in
routes.php
looks like...

Route::post('/content', 'Test@save');


I checked my
laravel.dev/content
and the following error message appears, I am not sure if it is normal...

MethodNotAllowedHttpException in RouteCollection.php line 218:


Any reason why my request is failing.

Answer

Do you actually have a controller for this? If not, you need to create one:

    class Test extends Controller
    {

       public function save()
       {
          ...
       }

    }

You also need to make sure that you are sending the csrf token with the request. Add the following to your blade template view form:

<input type="hidden" name="_token" value="{!! csrf_token() !!}">
Comments