我对 symfony2 很陌生,当用户单击 div 时,使用 ajax 加载 View 时遇到一些问题。使用 firebug 我可以看到数据已返回,但无法将结果附加到页面中。
我的代码: //默认 Controller
public function indexAction($num, Request $request)
{
$request = $this->getRequest();
if($request->isXmlHttpRequest()){
$content = $this->forward('PaginationBundle:Default:ajax');
$res = new Response($content);
return $res;
}
return $this->render('PaginationBundle:Default:index.html.twig', array('num' => $num));
}
public function ajaxAction()
{
return $this->render('PaginationBundle:Default:page.html.twig');
}
}
我的JS: 当点击#target时,我想在我的div中加载page.html.twig
$("div#target").click(function(event){
t = t +1;
$.ajax({
type: "POST",
cache: "false",
dataType: "html",
success: function(){
$("div#box").append(data);
}
});
});
我在 Controller 中使用 isXmlHttpRequest() 来检测是否是加载 ajaxAction 的 ajax 请求。我在 firebug 上得到了这个 View ,但它没有附加在我的 div#box 中。 div#box 存在于index.html.twig
先谢谢大家了
最佳答案
在你的 $("div#target").click(function(event) event 你没有在ajax调用中指定url参数,另一件事是你必须在'success'中指定一个参数 ajax调用的参数。
$("div#target").click(function(event){
t = t +1;
$.ajax({
type: "POST",
url: "{{path('yourpath-means header name in routing.yml')}}",
cache: "false",
dataType: "html",
success: function(result){
$("div#box").append(result);
}
});
});
希望这有帮助... 快乐编码
关于javascript - 使用ajax symfony2加载 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9516412/