0

This question has been asked before but non of the solutions work for me. This my code

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

and the script

<script type="text/javascript">
    $(document).ready(function () {
        $(".comment-rate-wrapper a img").on('click', function (e) {
            e.preventDefault();
            var item_id = 1;
            var url = "{{route('like.voteHandler', ':id')}}";
            url = url.replace(':id', item_id);
            //alert(url); I am sure thr url is correct and it outputs correctly
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                method: 'POST', // Type of response and matches what we said in the route
                url: url, // This is the url we gave in the route
                data: {
                    'item_id': item_id
                },
                success: function (result) { // What to do if we succeed
                    console.log(result);
                },
                error: function (jqXHR, textStatus, errorThrown) { // What to do if we fail
                    console.log(JSON.stringify(jqXHR));
                    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                }
            });
        })
    })
</script>

and this is the controller function

    public function voteHandler($item_id)
    {
        echo "sas";
        return "hi";
    }

But it always returns an empty array and I have no idea why it is not working.

Thanks

3
  • u should retrun response for ajax success/error Commented Mar 14, 2018 at 10:02
  • 2
    try this = return response(['msg' => $message, 'status' => 'success']); Commented Mar 14, 2018 at 10:03
  • @Jigs1212 I changed the controller function to both return result(['msg' => "hello", 'status' => 'success']); and return response(['msg' => "hello", 'status' => 'success']); but it doesn't work Commented Mar 14, 2018 at 10:06

3 Answers 3

1

Try this out in your controller

public function voteHandler(Request $request)
{
    return response()->json(["item_id" => $request->item_id]);
}

Edit: This was actually an error with the request method, change your route declaration to match your ajax request method POST

$router->post('{item_id}/voteHandler', 'LikeController@voteHandler')->name('like.voteHandler');

or change your ajax method to GET

Sign up to request clarification or add additional context in comments.

7 Comments

I tried return response()->json(["item_id" => 12]); and the js success to success: function (response) { console.log(response);} still not working
Try to add dd($request); before the return to see if it reaches that function
When I visit the url in the browser directly, it outputs the dd result, but in ajax request it doesn't
This is route declaration $router->get('{item_id}/voteHandler', 'LikeController@voteHandler')->name('like.voteHandler');
If you can reach it by url it means it is a GETroute, change either the route or the method in your ajax post
|
1

You can check Your Ajax request like below way.You should try to below way.

public function voteHandler(Request $request) {
 if(Request::ajax()){
        return response()->json(['status'=>'Ajax request']);
    }
    return response()->json(['status'=>'Http request']);
}

Comments

0

Change the Parameter and print the inputs

public function voteHandler(Request $request)
{
    print_r($request->all());
}

Make sure it reaches this method. Use firebug to debug the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.