jQuery Ajax POST请求作为GET与Laravel一起发送

时间:2022-10-08 23:59:20

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方面遇到了一些麻烦。我的项目在Laravel 5中,它在Apache上运行,并且启用了重写并且VerifyCsrfToken中间件已经到位。我正在尝试向我项目中的另一条路线发送POST请求。这是我的Ajax的样子:

$.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:

当我单击触发此Ajax的按钮时,我得到405:MethodNotAllowed响应。所以我进入routes.php并添加了一条GET路线。我还包括了我的POST路线:

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:

我收到'hello'消息,所以这是作为GET而不是POST发送的。我试图使用$ .post而不是$ .ajax强制POST,但我仍然得到相同的行为。为了更好的衡量,这是我的.htaccess文件:

<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.

由于重写规则(/ add-device),我也尝试了没有尾部斜杠的Ajax,但我得到了相同的'hello'消息。

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.

我在开发过程中测试了所有的Ajax请求(一半GET,一半POST),并且在与工匠一起工作时工作正常。切换到Apache之后我才遇到这个问题。我已进入项目的QA阶段,因此我将项目移至我们的开发服务器上,该服务器在Debian 8上运行Apache 2.4.10。

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

2 个解决方案

#1


7  

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!

所以我删除了尾随斜线,它突然起作用了。我不确定为什么会这样,但确实如此。我也把这个问题发给了Laracasts。我建议那些有同样问题的人通过评论阅读并通过这个其他线程,以便他们可以按照我的步骤。我认为我之前的一个步骤解决了一个问题,所以当我这次删除尾部斜杠时,它起作用了。谢谢大家的帮助!

#2


-1  

try this for your post route

试试这条路线

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

#1


7  

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!

所以我删除了尾随斜线,它突然起作用了。我不确定为什么会这样,但确实如此。我也把这个问题发给了Laracasts。我建议那些有同样问题的人通过评论阅读并通过这个其他线程,以便他们可以按照我的步骤。我认为我之前的一个步骤解决了一个问题,所以当我这次删除尾部斜杠时,它起作用了。谢谢大家的帮助!

#2


-1  

try this for your post route

试试这条路线

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