我正在尝试将 id 发布到 Controller 操作,但我收到此错误:(我使用的是 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()));
}
}