我正在使用 AJAX(或 triying)按类别过滤一段电影,这是我使用的 Jquery 代码:
$(function(){
$('.Action').click(function(e){
$("#moviesContainer").html("Loading movies...");
$.get($(this).attr("href"), function(data) {
$('#moviesContainer').html(data);
e.preventDefault();
});
return false;
});
});
和 Controller :
/**
* @Route(path="/show/category/{category}", name="aflam_movie_show")
* @Template("MoviesBundle:Default:allmovies.html.twig")
*/
public function showMovieByCategoryAction(Request $request, $category)
{
$em = $this->getDoctrine()->getManager();
$dql = "SELECT a FROM MoviesBundle:Movie a WHERE a.category=" .$category." ORDER BY a.created DESC";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1)/*page number*/,
5/*limit per page*/
);
$pagination->setUsedRoute('aflam_movie_show');
if($request->isXmlHttpRequest()){
return array('pagination' => $pagination);
}
}
我在按钮中设置路径是这样的:
{% for videocategory in ['Action','Drama','Comedy','Anime','Adults'] %}
<p class="lead"><a class="{{videocategory}}" href={{path('aflam_movie_show',{category:videocategory})}}">{{videocategory}}</a></p>
{% endfor %}
当我点击按钮时,它只显示“正在加载电影...”,这是什么问题?这是我第一次使用 symfony 进行 AJAX 调用,所以我有点迷路了。
最佳答案
只是完成给定答案的重要说明。
当您处理 AJAX 时,您必须使用打开的浏览器网络/控制台,以及“请求”选项卡中的 Symfony 分析器。
即:
通过这样做,如果出现错误,您将能够理解错误并修复它(给定的消息包含导致问题的文件和行)。
在您的情况下,错误肯定与您的 Controller 操作中缺少响应有关。
关于javascript - 使用带有 symfony 的 AJAX 按类别过滤部分时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35808035/