2015-09-08 75 views
0

我想在Sympfony2中實現無限滾動選項。簡單的請求到服務器Symfomny2

控制器:

jQuery的

$(function(){ 
    var data={ 
      type:'1'       
}; 
    var i = 0; 
    $(window).scroll(function(){ 
    //cuando llegas al final de la página 
    if (document.body.scrollHeight - $(this).scrollTop() <= $(this).height()){ 
      agregarContenido(); 
     } 
    }); 
    function agregarContenido(){ 
     //Agregar el siguiente contenido a mostrar 
     var path = "/"; 
     $.ajax({ 
     type: 'POST', 
     dataType : 'json', 
     data: data, 
     url: path, 
    success: function(response) { 
      console.log(response); 
     } 
    }); 
    } 
}); 

和使用routing.yml

index: 
path: /
    defaults: { _controller: UsuarioBundle:Default:index } 

的代碼不能正常工作,它永遠不會打印 「AJAX OK」。但是當我滾動時Ajax請求總是被髮送到服務器。

我的問題是,如何在結果中打印「ajax ok」?

回答

1

您在JS中指定的dataType存在問題。你的控制器返回Response對象這是普通的HTML,但你的JS對手期望json,所以這是行不通的。

嘗試設置dataTypehtml ...

0

在你的控制器,你應該讓

use Symfony\Component\HttpFoundation\JsonResponse; 

public function indexAction(Request $request) 
{ 
    if (!$request->isXmlHttpRequest()) { 
     return $this->render(
      'UsuarioBundle:Default:index.html.twig' 
     ); 
    } else { 
     $response = new JsonResponse(); 
     $response->setData(array(
      'status' => 1, 
      'result' => 'Ajax ok' 
     )); 

     return $response; 

    } 
} 
相關問題