doukuangxun5382 2016-11-22 10:46
浏览 75
已采纳

在ajax请求symfony之后刷新视图中的数据

I'am new in Ajax and I intregated one ajax request. The purpose is one user can like, unlike an article. Look my code :

controller

public function likeAction(Request $request, Article $article, $slug)
{
    if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
        throw $this->createAccessDeniedException();
    }

    if ($request->isXmlHttpRequest()) {
        $tokenStorage = $this->get('security.token_storage');
        $currentUser = $tokenStorage->getToken()->getUser();
        $likes = $article->getLikes();
        foreach ($likes->getUsers() as $user) {
            if ($user == $currentUser) {
                throw new \Exception('Vous aimez déjà cet article !');
            }
        }
        $likes->addUser($currentUser);
        $likes->setCount($likes->getCount() + 1);
        $em = $this->getDoctrine()->getManager();
        $em->persist($article);
        $em->flush();

        $count = $article->getLikes()->getCount();
        return new JsonResponse(array('data' => $count));
    }
    return $this->redirectToRoute('pm_platform_view', array('slug' => $slug));
}

route

pm_platform_like:
    path:      /like/{slug}
    defaults:
        _controller: PMPlatformBundle:Article:like

view

<a class="btn btn-blue-grey" id="like" role="button"></a>
<span class="counter" id="counter">{{ article.likes.count }}</span>

    <script>
        $( document ).ready(function() {
            $(document).on('click', '#like', function (e) {
                $this = $(this);
                $.ajax({
                    type: 'GET',
                    url: '{{ path('pm_platform_like', {slug: article.slug}) }}',
                    dataType: 'JSON',
                    data: {},
                    success: function() {
                      //refresh article.count here 
                    }
                });
            });
        });
    </script>

Currently the ajax request works and the "like" is persisted in database. But in my view nothing change, I have to "refresh" data, more precisly the like count attribute of article entity after the success of the ajax request. I need help for that.

  • 写回答

2条回答 默认 最新

  • doujia6503 2016-11-22 11:20
    关注

    Your AJAX request is already send total counts in response. So, all you need to update "counter" div with total like count.

    objResponse holds response of ajax request and total like count would be stored inobjResponse.data .

    success: function(objResponse) {  // "objResponse" is response os ajax request
        $("#counter").html(objResponse.data);
        ^^
    }
    

    Full Code

    <a class="btn btn-blue-grey" id="like" role="button"></a>
    <span class="counter" id="counter">{{ article.likes.count }}</span>
    
    <script>
        $( document ).ready(function() {
            $(document).on('click', '#like', function (e) {
                $this = $(this);
                $.ajax({
                    type: 'GET',
                    url: '{{ path('pm_platform_like', {slug: article.slug}) }}',
                    dataType: 'JSON',
                    data: {},
                    success: function(objResponse) {  // "objResponse" is response os ajax request
                      //refresh article.countcounter here 
                      $("#counter").html(objResponse.data);
                      ^^
                    }
                });
            });
        });
    </script>
    

    Read more about ajax

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置