当我使用ajax(jquery)发送数据时,我没有在laravel 5.1中找到控制器中没有形式的值

时间:2015-12-14 11:38:19

标签: laravel

这是我的刀片代码:

<script>
$.ajaxSetup({
   headers: {
       'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$(document).ready(function(){
    $(window).load(function(){
        var topic_id = $("#topic").text();
        var post = $("#post").text();
        var url_id = "/posts/"+post+"/readStatus";
        $("#topic").hide();
        var users_id = $("#user").text();
        $("#user").hide();
        $.ajax({
            async:true,
            method:"post",
            url:url_id,
            topic_id:topic_id,
            users_id:users_id,
            processData: false,
            contentType: false,
            success:function(response){
                console.log(response);
                $("#message").html(response); 
            }
        },"json");
    });
});

Route::post('/posts/{post_id}/readStatus',function(){
    if(Request::ajax()){
    //In routes.php
        $post =  App\Post::find($post_id);
        if(auth()->guest())
            return "Not user Not read";
        else{
            $user = App\User::find(auth()->user()->id);
            $post->attach($user);
            return "Read";
        }
        //return Response::json(Request::all());
    }
});

我收到500内部服务器错误。我发现url中的$ post_id并没有保留任何值。如果我在$ post_id中给出任何常量值,如1,2 ......我得到答案。我不想使用表单来实现。我在页面加载时给出了ajax调用。

1 个答案:

答案 0 :(得分:1)

您必须将$post_id定义为函数参数。

Route::post('/posts/{post_id}/readStatus',function($post_id){
    ...
});