[英]Jquery AJAX Post: 500 (Internal Server Error)? in Symfony 3
I am trying post an id to Controller action but I am getting this error : (I am using Symfony 3). 我正在尝试将一个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 (Internal Server Error) jquery-1.11.0.min.js:4 POST http://localhost/plate/web/app_dev.php/province_ajax_call?province_id = 2500(内部服务器错误)
Here is my ajax code : 这是我的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');
}
})
})
})
Here is My controller code : 这是我的控制器代码:
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()));
}
}
I resolved my problem by adding 我通过添加解决了我的问题
use Symfony\Component\HttpFoundation\JsonResponse;
in the controller. 在控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.