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

时间:2015-05-06 17:51:47

标签: jquery ajax apache laravel

我在使用Ajax方面遇到了一些麻烦。我的项目位于Laravel 5,并且已在Apache上运行,rewrite已启用且VerifyCsrfToken中间件已到位。我试图将POST请求发送到我项目中的另一条路径。这是我的Ajax的样子:

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

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

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

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

我得到了'#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>

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

我在开发过程中测试了所有Ajax请求(半个GET,一半POST),并且在使用artisan时工作正常。切换到Apache后,我才出现此问题。我已进入项目的质量保证阶段,因此我将项目移至我的开发服务器上,该服务器在Apache 2.4.10上运行Debian 8

任何人对于发生了什么以及如何解决这些问题都有任何想法?

其他内容

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

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:0)

从ajax请求网址中删除了斜杠对我有用

答案 2 :(得分:-1)

试试这条路线

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