Trying to do AJAX post to controller action and getting an action not defined or 405 error

Posted 1 year ago by FrenchFryNinja

Trying to do an AJAX post. I've hard coded the url to either '/orders' and also used {{ action('[email protected]') }}. The former results in a 405 ajax response and the latter throws an action not defined error and won't load the page.

Here's the web.php:

Route::post('/orders, [email protected]');

There are no other Route::post() that lead to OrderController so no conflict there.

And the AJAX:

$.ajax({
                method: 'POST', 
                url: '/orders',
                data: getOrderJSON(),
                cache: false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function (response) {
                    console.log(response);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(JSON.stringify(jqXHR));
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                }
            });

It gets called from a button. Nothing too fancy in the button:

                    <a class="btn btn-outline-red float-right" onclick="processOrder()">Submit Order</a>

I'm really lost here. I've checked the namespacing and its all correct. I've used other methods and they at least work, though the response is essentially gibberish. The page will load. I've done all of these (not necessarily in this order):

composer dump-autoload
php artisan clear-compiled
php artisan cache:clear
php artisan route:clear
php artisan optimize
composer update

And I'm sort of lost here. Everything I've read I have the namespaces correct and the routes file setup correctly, but I'm not able to see the route. Any ideas?

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