Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax请求symfony后刷新视图中的数据_Javascript_Php_Ajax_Twig_Symfony - Fatal编程技术网

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

Javascript ajax请求symfony后刷新视图中的数据,javascript,php,ajax,twig,symfony,Javascript,Php,Ajax,Twig,Symfony,我是Ajax新手,我提出了一个Ajax请求。目的是一个用户可以喜欢,而不是一篇文章。看看我的代码: 控制器 public function likeAction(Request $request, Article $article, $slug) { if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { throw $this

我是Ajax新手,我提出了一个Ajax请求。目的是一个用户可以喜欢,而不是一篇文章。看看我的代码:

控制器

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));
}
路线

看法


{{article.likes.count}
$(文档).ready(函数(){
$(文档).on('click','like',函数(e){
$this=$(this);
$.ajax({
键入:“GET”,
url:{{path('pm_platform_like',{slug:article.slug}}}},
数据类型:“JSON”,
数据:{},
成功:函数(){
//刷新文章。在这里计数
}
});
});
});

目前ajax请求可以工作,“like”被持久化到数据库中。但在我看来,没有什么变化,我必须“刷新”数据,更准确地说,在ajax请求成功后,文章实体的like count属性。我需要帮助。

假设您有另一个类似count的ajax请求

success: function(response) {
        //Recall function here that gets like count.
        fnGetLikeCount();
    }
编辑:

您可以发布类似count的函数/ajax请求吗

编辑2:


是的,您可以从控制器发送响应,并可以在标签/div或您正在使用的任何东西中分配/设置计数。

您的
AJAX
请求已经在响应中发送总计数。因此,您只需使用total like count更新“counter”
div

success: function(response) {
        //Recall function here that gets like count.
        fnGetLikeCount();
    }
objResponse
保存
ajax
请求的响应,类总数将存储在
objResponse.data

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

<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>

在ajax成功的情况下,您可以调用函数来获得类似的计数。我不明白,您可以编写一个示例吗?我没有这个ajax请求。您是否认为可以在Json响应中(在控制器中)返回like计数?请参阅编辑的答案,您可以从控制器发送响应。
<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>