2017-07-28 144 views
0

我試圖發佈一個id到控制器動作,但我得到這個錯誤: (我正在使用Symfony 3)。Jquery AJAX Post:500(內部服務器錯誤)?在Symfony 3

jQuery的1.11.0.min.js:4 POST http://localhost/plate/web/app_dev.php/province_ajax_call?province_id=2 500(內部服務器錯誤)

這裏是我的Ajax代碼:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
{{ form(form_account) }} 


<script> 
$(document).ready(function(){ 
    $('#appbundle_account_Province').change(function(){ 
     var val = $('#appbundle_account_Province').val(); 
     $.ajax({ 
      type: "POST", 
      url: "{{ url('province_ajax_call') }}?province_id=" + val, 
      success: function(data){ 
       $('#appbundle_account_City').html(''); 
       $.each(data, function(key, value){ 
        $('#appbundle_account_City').append('<option value="'+ key +'">'+ value +'</option>'); 
       }) 
       console.log('succes'); 
      } 
     }) 
    }) 
}) 

這裏是我的控制器代碼:

public function provinceAjaxCallAction(Request $request) 
{ 

    if (!$request->isXmlHttpRequest()) 
    { 
     throw new NotFoundHttpException(); 
    } 

    $id = $request->query->get('province_id'); 
    var_dump($id); 

    $cities = $this 
       ->getDoctrine() 
       ->getManager() 
       ->getRepository('AppBundle:City') 
       ->findByProvinceId($id); 

    $result = array(); 
    foreach($cities as $city){ 
     $result[$city->getId()] = $city->getName(); 
    } 
    return new JsonResponse($result); 
} 

public function indexAction(Request $request) 
{ 
    $account = new Account(); 

    $form = $this->createForm(AccountType::class, $account); 
    $form->handleRequest($request); 

    if($form->isValid()) 
    { 
     $this->getDoctrine()->getManager()->persist($account); 
     $this->getDoctrine()->getManager()->flush(); 

     return $this->redirectToRoute('test_registration_homepage'); 
    } 

    return $this->render('default/index.html.twig', array('form_account' => $form->createView())); 


} 
} 
+0

它可以是任何東西,從配置文件中的錯字開始。 你可以看到日誌 - 他們會告訴你到底發生了什麼問題。 – matiit

+0

我檢查日誌,但我什麼都不明白 – Sabra

+0

@Sabra,你檢查了哪些日誌?也許你檢查了錯誤的日誌?如果你正在檢查你的PHP日誌,並且不理解它,請在這裏發佈錯誤消息,以便我們可以讓你知道它的含義。如果您不瞭解如何閱讀錯誤日誌消息,那麼對於正在進行的任何項目,您都不會感到很滿意。 – kojow7

回答

0

我解決了我的問題,通過添加

use Symfony\Component\HttpFoundation\JsonResponse; 

在控制器中。

相關問題