2016-08-22 120 views
0

我是Symfony中的新手我想做一些操作,我可以通過Ajax獲取實體主題中所有元素的列表,但答案仍然是「未定義」此處代碼文本強symfony響應json ajax

VUE

$(document).ready(function() { 
     var $theme = $('#theme'); 
     $theme.append('<option value="">Choisir un thème</option>'); 
     $.ajax({ 
      type: 'post', 
      url: '{{ path('web_site_liste_theme_cmb') }}', 
      dataType: 'json', 
      beforeSend: function() { 
       $('#themediv').append('<div id="loading" style=" float: left; display: block;"><img src="{{ asset('bundles/BackBundle/img/loadingicon.gif') }}"/></div>'); 
      }, 
      success: function (json) { 
       {#$('#theme').append({{ dump(json)}});#} 
       console.log(json.value); 
       $.each(json, function (index, value) { 

        //console.log(value); 
        $('#theme').append('<option value="' + value.id + '">' + value.name + '</option>'); 
        $('#loading').remove(); 
       }); 
      } 
     }); 
    }); 

控制器

public function ListeThemeAction(Request $request) 
{ 

    $em = $this->getDoctrine()->getEntityManager(); 
    if ($request->isXmlHttpRequest()) { 
     $themes = $em->getRepository('WebSiteBackBundle:theme'); 
     $themes = $themes->findAll(); 
     //var_dump($themes); 

     return new JsonResponse($json); 
    } 
    return new JsonResponse('no results found', Response::HTTP_NOT_FOUND); // constant for 404 

} 

服務器重新sponse是200 OK,一切似乎工作,我也有同樣數量的數據庫中的數據,但我無法讀取的對象值

這裏是: console.log(json)

+0

你能提供一些移動信息嗎?服務器的響應是什麼?什麼是'未定義'? 「json.value」? – carmel

+0

服務器的響應是200 OK,一切似乎工作,我有數據庫中的數據相同的數量,但我沒有讀對象值 –

回答

0

好吧,我找到了答案,我不得不改變控制器,使其發生。 謝謝Yonel! 這裏是一個鏈接,幫助我Json in controller

控制器

public function ListeThemeAction(Request $request) 
{ 
$output=array(); 
    $em = $this->getDoctrine()->getEntityManager(); 
    if ($request->isXmlHttpRequest()) { 
     $themes = $em->getRepository('WebSiteBackBundle:theme'); 
     $themes = $themes->findAll(); 
     foreach ($themes as $theme){ 

      $output[]=array($theme->getId(),$theme->getName()); 
     } 
    /* var_dump($themes); 
     $json = json_encode($themes); 

     $response = new Response();*/ 
     //   return $response->setContent($json); 
     return new JsonResponse($output); 

    } 
    return new JsonResponse('no results found', Response::HTTP_NOT_FOUND); 
} 

Vue公司

 $(document).ready(function() { 
     var $theme = $('#theme'); 
     $theme.append('<option value="">Choisir un thème</option>'); 
     $.ajax({ 
      type: 'post', 
      url: '{{ path('web_site_liste_theme_cmb') }}', 
      dataType: 'json', 
      beforeSend: function() { 
       $('#themediv').append('<div id="loading" style=" float: left; display: block;"><img src="{{ asset('bundles/BackBundle/img/loadingicon.gif') }}"/></div>'); 
      }, 
      success: function (json) { 

       console.log(json); 
       $.each(json, function (index, value) { 

        //console.log(value); 
        $('#theme').append('<option value="' + value[0]+ '">' + value[1] + '</option>'); 
        $('#loading').remove(); 
       }); 
      } 
     }); 
    }); 

希望它能幫助別人。謝謝大家的回答,它幫助了我很多

2

有很多方法可以做到那我告訴你其中的一個。

首先,準備好你的數據從控制器發送,並返回一個JsonResponse實例與您的數據:

public function fooAction() 
{ 
    $data = ['foo1' => 'bar1', 'foo2' => 'bar2']; 

    return new JsonResponse($data); //or $this->json($data) since Symfony 3.1 
} 

JsonResponse告訴給發送的數據必須被解釋爲JSON的瀏覽器,否則這些數據默認情況下被解釋爲純文本/文本。

其次,使用相同的數據結構從JavaScript回調函數訪問:

$.post('{{ path('web_site_liste_theme_cmb') }}', function (data) { 
    console.log(data['foo1']); //output bar1 
    console.log(data.foo2); //output bar2 
}); 

在你的問題發送您的數據是對象的數組,所以你需要循環這個變量「JSON」(數組)和訪問對象的每個屬性。

+0

我怎樣才能循環到對象的每個屬性。我猜我已經在「$ .each(json,function(index,value){...」)中做過了嗎? –

+0

是的,但它取決於您的WebSiteBackBundle:主題實體,如果它包含「id」和「value」屬性,否則,你應該在發送你的控制器的json響應之前準備好這些數據。 – yceruto

+0

你說得對,它對我很有用,謝謝你! –