Jquery Symfony2:AJAX提交500错误“;“变量不存在”;
我有一个细枝视图,它包含三种不同实体的几种形式:面积、学期、常规小时。下面的示例是区域实体的edit.html.twig文件Jquery Symfony2:AJAX提交500错误“;“变量不存在”;,jquery,ajax,symfony,symfony-forms,Jquery,Ajax,Symfony,Symfony Forms,我有一个细枝视图,它包含三种不同实体的几种形式:面积、学期、常规小时。下面的示例是区域实体的edit.html.twig文件 {% block body -%} <h1 class="page-header">HoursArea edit</h1> ... <div class="row"> <div class="col-xs-12"> <h2>Regular Hou
{% block body -%}
<h1 class="page-header">HoursArea edit</h1>
...
<div class="row">
<div class="col-xs-12">
<h2>Regular Hours</h2>
{{ form(semester_form) }}
Sunday: {{ form(day_0) }}
Monday: {{ form(day_1) }}
Tuesday: {{ form(day_2) }}
Wednesday: {{ form(day_3) }}
Thursday: {{ form(day_4) }}
Friday: {{ form(day_5) }}
Saturday: {{ form(day_6) }}
</div>
</div>
{% endblock %}
{% block documentReady %}
$('.regularHours_form').submit(function(event){
event.preventDefault();
ajaxObject = {
url: $("form").attr("action"),
type: 'PUT',
dataType: 'json',
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({"openTime":$("#appbundle_hoursrgular_openTime", this).val(), "closeTime":$("#appbundle_hoursrgular_closeTime", this).val(), "is24hour":$("#appbundle_hoursrgular_is24Hour", this).is(':checked'), "isClosed":$("#appbundle_hoursrgular_isClosed", this).is(':checked')})
};
$.ajax(ajaxObject)
.success(function(data,status,xhr) {
console.log( status );
})
.fail(function(data,status,xhr) {
console.log( status );
})
.always(function(data,status,xhr) {
console.log( status );
});
});
{{ parent() }}
{% endblock %}
此外,每日工时表应通过以下方式提交给常规工时控制员:
/**
* Edits an existing HoursRegular entity.
*
* @Route("/{id}", name="hoursregular_update")
* @Method("PUT")
*/
public function updateAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:HoursArea')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find HoursRegular entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
//$semesterForm
if ($editForm->isValid()) {
$em->flush();
$serializer = $this->get('serializer');
$serialized = $serializer->serialize($entity, 'json');
return new JsonResponse("All is well!", 204);
}
return new JsonResponse("Oh no something went wrong!", 400);
}
显然,我甚至没有到达更新代码,因为我收到的是500错误,而不是204或400错误,因为XHTML 1.x表单只支持GET和POST。GET和POST是“method”属性唯一允许的值。因此,我建议您使用POST HTTP动词而不是PUT
由于XHTML 1.x表单仅支持GET和POST,希望获得此帮助。GET和POST是“method”属性唯一允许的值。因此,我建议您使用POST HTTP动词而不是PUT
希望获得此帮助您可以发布这两个操作的路由定义吗?你能检查一下路由在页面加载时是否生成良好吗?@Matteo我已经将路由添加为annotationsHi@Ravioli87。我想你不能用PUT HTML动词来填充表单。你能试试PostHTML动词吗?谢谢@Matteo。现在我至少达到了400错误。在这种情况下我不允许使用PUT有什么特别的原因吗?我正在更新一个实体,以便它应该是要使用的正确HTTP方法,对吗?您可以发布这两个操作的路由定义吗?你能检查一下路由在页面加载时是否生成良好吗?@Matteo我已经将路由添加为annotationsHi@Ravioli87。我想你不能用PUT HTML动词来填充表单。你能试试PostHTML动词吗?谢谢@Matteo。现在我至少达到了400错误。在这种情况下我不允许使用PUT有什么特别的原因吗?我正在更新一个实体,以便它应该是正确的HTTP方法,对吗?
public function createEditForm(HoursRegular $entity)
{
$form = $this->createForm(new HoursRegularType(), $entity, array(
'action' => $this->generateUrl('hoursregular_update', array('id' => $entity->getId())),
'method' => 'PUT',
'attr' => array('class' => 'regularHours_form')
));
$form->add('submit', 'submit', array('label' => 'Save Day'));
return $form;
}
/**
* Edits an existing HoursRegular entity.
*
* @Route("/{id}", name="hoursregular_update")
* @Method("PUT")
*/
public function updateAction(Request $request, $id){
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:HoursArea')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find HoursRegular entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
//$semesterForm
if ($editForm->isValid()) {
$em->flush();
$serializer = $this->get('serializer');
$serialized = $serializer->serialize($entity, 'json');
return new JsonResponse("All is well!", 204);
}
return new JsonResponse("Oh no something went wrong!", 400);
}