2017-07-28 146 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; 

在控制器中。

相关问题