在laravel 5返回错误阿贾克斯后500(内部服务器错误)错误、服务器、laravel、阿贾克斯后

2023-09-10 20:21:44 作者:站着茅坑不拉屎

这是laravel 5(见下文)我的测试AJAX

this is my test ajax in laravel 5 (refer below)

$("#try").click(function(){
    var url = $(this).attr("data-link");
    $.ajax({
        url: "test",
        type:"POST",
        data: { testdata : 'testdatacontent' },
        success:function(data){
            alert(data);
        },error:function(){ 
            alert("error!!!!");
        }
    }); //end of ajax
});

和触发链接

<a href="#" id="try" data-link="{{ url('/test') }}">Try</a>

和我的路线

Route::post('test', function()
{
    return 'Success! ajax in laravel 5';
});

但它给我的错误,当我运行控制台在谷歌Chrome和它不返回预期的响应回归的成功阿贾克斯laravel 5!;

but it gives me error when I run the console in google chrome and it doesnt return the expected response "return 'Success! ajax in laravel 5';"

POST http://juliver.laravel.com/test 500(内部服务器错误)

POST http://juliver.laravel.com/test 500 (Internal Server Error)

什么是错/问题我的code?什么即时通讯失踪?

whats wrong/problem to my code? anything im missing?

推荐答案

虽然这个问题存在了一段时间,但没有公认的答案给出我想为您指出解决方案。因为你发送带有Ajax和presumably仍然使用CSRF中间件,您需要提供一个可选的头你的要求。

While this question exists for a while, but no accepted answer is given I'd like to point you towards the solution. Because you're sending with ajax, and presumably still use the CSRF middleware, you need to provide an optional header with your request.

添加元标记的每一页(或主布局):&LT; META NAME =CSRF令牌值={{csrf_token()}}&GT;

Add a meta-tag to each page (or master layout): <meta name="csrf-token" value="{{ csrf_token() }}">

和添加到您的JavaScript文件(或部分页面内):

And add to your javascript-file (or section within the page):

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

请参阅 http://laravel.com/docs/master/routing #CSRF-X-CSRF令牌了解详细信息。