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?
Aucun commentaire:
Enregistrer un commentaire