dpkk8687
2018-03-14 10:00
浏览 98
已采纳

Laravel 5.5 ajax成功函数返回空数组

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条回答 默认 最新

相关推荐 更多相似问题