JQuery AJAX POST request sent as GET with Laravel

I have small problems with Ajax. My project is in Laravel 5 , and it runs on Apache and rewrite turned on, and the VerifyCsrfToken in place. I am trying to send a POST request to a different 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 launches this Ajax, I get a 405: MethodNotAllowed response 405: MethodNotAllowed . So I went into routes.php and I added a GET route. I also included my POST route:

 Route::get('add-device', function() { return 'hello'; }); Route::post('add-device', [ 'middleware' => 'auth', 'uses' => ' FormController@add _device' ]); 

I get a hello message, so it is sent as GET instead of POST. I tried using $.post instead of $.ajax to force 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 Ajax without a trailing slash due to 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 great when serving artisan . I only had this problem after switching to Apache . I went to the QA phase of my project, and so I transferred the project to our development server, which runs Apache 2.4.10 on Debian 8 .

Does anyone have any ideas on what is happening and how to solve 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 
+7
jquery ajax apache laravel
source share
2 answers

So, I removed the trailing slash, and it suddenly worked. I'm not sure why this happened, but it was. I posted this question to Laracasts . I would recommend that those who had the same problem, I read the comments through this other thread too so that they can follow my steps. I think one of my previous steps solved the problem, so when I removed the trailing slash this time, it worked. Thank you all for your help!

+7
source share

try this for your mail route

 Route::post('add-device', [ 'as' => 'add_device.post' 'middleware' => 'auth', 'uses' => ' FormController@add _device' ]); 
-one
source share

All Articles