我是symfony2的新手,当用户点击div时,我在使用ajax加载视图时遇到了一些问题.使用firebug我可以看到返回的数据,但我无法将结果附加到页面中.
我的代码://默认控制器
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');
}
}
Run Code Online (Sandbox Code Playgroud)
我的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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用isXmlHttpRequest()来检测它是否是加载ajaxAction的ajax请求.我在firebug上看到了那个视图,但它没有附加在我的div#box中.div#box存在于index.html.twig中
提前谢谢大家
在$("div#target")中.单击(函数(事件)事件,你没有在ajax调用中指定url参数,另一件事是你必须在ajax调用的'success'参数中指定一个参数.
$("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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
希望这有帮助......快乐的编码