Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax POST request being sent as GET with Laravel

I'm having a little trouble with Ajax. My project is in Laravel 5 and it's running on Apache and rewrite is enabled and the VerifyCsrfToken middleware is in place. I'm trying to send a POST request to another route inside my project. Here's what my Ajax looks like:

$.ajax({
    url: '/add-device/',
    type: 'POST',
    data: form_data,
    success: function(data)
    {
        console.log(data);
    },
        error: function(data)
    {
        console.log(data);
    }
});

When I click the button that triggers this Ajax, I get a 405: MethodNotAllowed response. So I went into routes.php and I added a GET route. I've also included my POST route:

Route::get('add-device', function()
{
    return 'hello';
});

Route::post('add-device', [
    'middleware' => 'auth',
    'uses' => 'FormController@add_device'
]);

I get the 'hello' message, so this is being sent as GET instead of POST. I tried to use $.post instead of $.ajax to force the POST but I still get the same behavior. For good measure, here is my .htaccess file:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I also tried the Ajax without the trailing slash because of the rewrite rule (/add-device) but I get the same 'hello' message.

I tested all of my Ajax requests (half GET, half POST) during development and they worked fine while being served with artisan. I've only had this problem come up after switching to Apache. I've moved into the QA phase of my project and so I moved the project onto our development server, which is running Apache 2.4.10 on Debian 8.

Anyone have any ideas on what is going on and how to resolve it?


Additional Content

Exception trace:
 () at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:901
 Illuminate\Foundation\Application->abort() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:21
 abort() at /home/debian/public_html/ZipPrinter/app/Handlers/Events/AbortTheRequest.php:28
 App\Handlers\Events\AbortTheRequest->handle() at n/a:n/a
 call_user_func_array() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:327
 Illuminate\Events\Dispatcher->Illuminate\Events\{closure}() at n/a:n/a
 call_user_func_array() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:218
 Illuminate\Events\Dispatcher->fire() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:650
 event() at /home/debian/public_html/ZipPrinter/app/Services/ZipHelper.php:56
 App\Services\ZipHelper->__construct() at /home/debian/public_html/ZipPrinter/app/Services/DashHelper.php:43
 App\Services\DashHelper->__construct() at /home/debian/public_html/ZipPrinter/app/Http/Controllers/DashController.php:28
 App\Http\Controllers\DashController->__construct() at n/a:n/a
 ReflectionClass->newInstanceArgs() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Container/Container.php:817
 Illuminate\Container\Container->build() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Container/Container.php:656
 Illuminate\Container\Container->make() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:644
 Illuminate\Foundation\Application->make() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:161
 Illuminate\Foundation\Console\RouteListCommand->getControllerMiddleware() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:142
 Illuminate\Foundation\Console\RouteListCommand->getMiddleware() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:109
 Illuminate\Foundation\Console\RouteListCommand->getRouteInformation() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:89
 Illuminate\Foundation\Console\RouteListCommand->getRoutes() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:75
 Illuminate\Foundation\Console\RouteListCommand->fire() at n/a:n/a
 call_user_func_array() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Container/Container.php:523
 Illuminate\Container\Container->call() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Console/Command.php:115
 Illuminate\Console\Command->execute() at /home/debian/public_html/ZipPrinter/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257
 Symfony\Component\Console\Command\Command->run() at /home/debian/public_html/ZipPrinter/vendor/laravel/framework/src/Illuminate/Console/Command.php:101
 Illuminate\Console\Command->run() at /home/debian/public_html/ZipPrinter/vendor/symfony/console/Symfony/Component/Console/Application.php:874
like image 864
beznez Avatar asked May 06 '15 17:05

beznez


1 Answers

So I removed the trailing slash and it suddenly worked. I'm not sure why exactly it did, but it did. I posted this question to Laracasts as well. I would recommend that those having the same issue I did to read through the comments and through this other thread so they can follow my steps. I think one of my previous steps resolved a problem so that when I removed the trailing slash this time, it worked. Thanks everyone for your help!

like image 192
beznez Avatar answered Oct 21 '22 10:10

beznez