Ad

When I Send Data With Ajax(jquery) I Don't Find The Values In Controller Without Form In Laravel 5.1

- 1 answer

This is my blade code:

<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());
    }
});

I get an 500 Internal server error .I found that $post_id in url doesn't hold any value inside.If I give any constant value in $post_id like 1,2... I get the answer.I don't want to use forms to achieve.I gave an ajax call as page loads.

Ad

Answer

You have to define $post_idas your function argument.

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