Skip to content
Advertisement

Why won’t my simple get request with axios return what the controller’s returning?

I’m learning Laravel with React.js but I can’t figure out why my simple get request won’t work. I get a 404 error. I’ve seen a similar question asked on SO and I’ve tried running composer require barryvdh/laravel-cors on the command line but it still doesn’t work.

How can I rectify this?

Here’s react code:

constructor(props) {
    super(props);
    this.axiosGet = this.axiosGet.bind(this);
}

axiosGet(e) {
    e.preventDefault();
    axios.get('/thank-you')
        .then(response => {
            console.log(response);
        });
}

render() {
    <Form onSubmit={this.axiosGet}>
    ...
    </Form> 
}

Here’s api.php

use AppHttpControllersMailController;

Route::get('/thank-you', [MailController::class,'index']);

Here’s MailController.php

public function index() {
    return "Testing";
}

Advertisement

Answer

your route is /api/thank-you. When you are using API routes. API comes in front of your routes.

Change your axios.get('/thank-you') as axios.get('/api/thank-you')

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement