我對symfony2非常陌生,當用戶單擊div時,我遇到了一些使用ajax加載視圖的問題。使用螢火蟲我可以看到數據被返回,但我無法在頁面中追加結果。使用ajax加載視圖symfony2
我的代碼:// 默認控制器
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);
}
});
});
我在我的控制器中使用isXmlHttpRequest()來檢測它是否是加載ajaxAction的ajax請求。我在firebug上看到了這個視圖,但它並沒有附加在我的div#框中。存在於index.html.twig DIV#箱
謝謝大家提前
你爲什麼要這麼做$請求= $請求 - > Request()方法?$請求已經被設置 – Cerad 2012-03-01 15:50:12
謝謝,我還有點困惑我需要閱讀文檔,更加註意細節 – rpa 2012-03-02 01:56:31