Laravel 5 AJAX 500错误发布

时间:2015-04-14 06:33:50

标签: jquery ajax laravel

我正在尝试使用Laravel 5和AJAX https://www.youtube.com/watch?v=PRCm-7mEDkY的本教程,这显然有效,但是在Post Request上收到500内部服务器错误。我究竟做错了什么?我只是想了解一个简单的例子并从那里开始工作。

HTML

<a href="#" id="get">Get</a>
<hr>
<form action="#">
    <input type="text" name="name">
    <button type="submit">Send</button>
</form>

JS

$(function() {
    $('#get').click(function(e){

        e.preventDefault();

        $.get('categories', function(data) {
            console.log(data);
        });
    });

    $('form').submit(function(e) {
        e.preventDefault();

        $.post('categories', {name:'name anything'}, function(data) {
            console.log(data);
        });
    });

});

路线

Route::post('/categories', function() {
    if(Request::ajax() ) {
        var_dump(Input::all());
        return 'ajax post request';
    }
});

存储日志

#0 /Users/neil/Sites/demo02/app/Http/Middleware/VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#2 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(55): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(61): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#7 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(40): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#14 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(111): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#15 /Users/neil/Sites/demo02/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(84): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#16 /Users/neil/Sites/demo02/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#17 /Users/neil/Sites/demo02/server.php(21): require_once('/Users/neil...')
#18 {main}

3 个答案:

答案 0 :(得分:3)

在laravel 5中,所有发布请求都需要csrf令牌。您必须在帖子请求正文中添加_token字段,或添加标题X-CSRF-TOKEN

更多信息

http://laravel.com/docs/master/routing#csrf-protection

一种可能的解决方案[如果JS在刀片模板中]。

$(function() {
$('#get').click(function(e){

    e.preventDefault();

    $.get('categories', function(data) {
        console.log(data);
    });
});

$('form').submit(function(e) {
    e.preventDefault();

    $.post('categories', {name:'name anything', _token: '{{ csrf_token() }}'}, function(data) {
        console.log(data);
    });
});

});

答案 1 :(得分:2)

正如艾哈迈德所说,如果脚本在刀片模板中,这将有效。

但是,如果您愿意,可以将令牌添加到<head>app.blade.php标记内的模板中:

<meta name="csrf-token" content="{{ csrf_token() }}" />

小心你必须使用刀片。 然后,您需要在脚本中设置ajax:

$(function() {

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $('#get').click(function(e){

        e.preventDefault();

        $.get('categories', function(data) {
            console.log(data);
        });
    });

    $('form').submit(function(e) {
        e.preventDefault();

        $.post('categories', {name:'name anything'}, function(data) {
            console.log(data);
        });
    });
});

关于您的405获取错误,您是否创建了get路由?

Route::get('categories', function() {
    return "Hello there!";
});

答案 2 :(得分:0)

我有一系列问题让AJAX表单提交与Laravel 5一起工作。这大致是我在工作之前必须解决的一系列问题:

  • 确保您使用AJAX请求提交CSRF令牌。
  • 确保您的路由确实设置正确(有一段时间我将我的AJAX请求发布到一个不正确的端点:这是一条路由,已经设置为GET主路由的别名,但不是POST的路由)
  • 确保您在AJAX请求的数据对象中提交的字段与您的端点所期望的字段完全匹配(在填充数据对象时我得到其中一个字段的名称,并且导致500错误,因为我的模型没有设置(必填)字段。