im试图使用AJAX向控制器发送HMTL表单的数据,但是控制器未在错误显示的情况下接收数据。
html表格:
<form id="evento" name="evento" method="post" >
<div class="form-group">
<label for="evento-titulo" class="col-form-label">Título:</label>
<input type="text" class="form-control" id="evento_titulo" name="evento_titulo">
</div>
<div class="form-group">
<label for="evento_descripcion" class="col-form-label">Descripción:</label>
<textarea class="form-control" id="evento_descripcion" name="evento_descripcion"></textarea>
</div>
<div class="form-group">
<label for="evento_inicio" class="col-form-label">Inicio:</label>
<input type="datetime-local" class="form-control" id="evento_inicio" name="evento_inicio">
</div>
<div class="form-group">
<label for="evento_fin" class="col-form-label">Fin:</label>
<input type="datetime-local" class="form-control" id="evento_fin" name="evento_fin">
</div>
<div class="form-group">
<label for="evento_checkmail" class="col-form-label">
<input type="checkbox" id="chkmail" name="chkmail" style="vertical-align:middle;">
Añadir correo                
Color:
<input type="color" id="color" name="color" style="vertical-align:middle;">
</label>
<input type="email" class="form-control" id="evento_mail" name="evento_mail" disabled="disabled">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-danger" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-outline-success" id="save_event" name="save_event">Confirmar</button>
</div>
</form>
通过Ajax将表单数据传递给控制器:
$("#evento").submit(function(){
var data = $('#evento').serialize();
$.ajax({
type: "POST",
data: data,
url: {{ render( controller('CalendarBundle:Calendar:addEvent')) }},
success: function(data)
{
console.log($data);
alert('OK');
}
}); });
控制器:
public function addEventAction() {
$em = $this->getDoctrine()->getManager();
//fetch the POST Data
$request = $this->get('request_stack')->getCurrentRequest();
$data = $request->request->all();
$evento = new Evento();
$evento->setBeginat(new DateTime($data["evento_inicio"]));
$evento->setEndat(new DateTime($data["evento_fin"]));
$evento->setTitle($data["evento_titulo"]);
$evento->setMail($data["evento_mail"]);
$evento->setDescription($data["evento_descripcion"]);
$evento->setColor($data["color"]);
$em->persist($evento);
$em->flush();
//return response
$serializedEntity = $this->container->get('serializer')->serialize($evento, 'json');
$response = new Response($serializedEntity);
$response->headers->set('Content-Type', 'application/json');
return $response;
}}
这是错误消息
在渲染模板的渲染过程中抛出了一个例外 ("通知:未定义的索引:Evenso_inicio"(。
您的ajax URL属性不是好的,您应该只调用与控制器Action AddeDeventAction((关联的URL(路由(。
假设您的AddEventAction路线为"/event/add",然后放置url: "/event/add"
,甚至您有此路线的名称,然后放置url: "{{ path('name_of_your_route') }}"
AJAX请求的URL属性必须是发送请求的有效地址: - (
您的AJAX请求是在application/json
内容类型中发送的,但您的控制器期望application/x-www-form-urlencoded
。
尝试json_decode()
ING $request->getContent()
获取您的$data
。
查看此捆绑包以从JavaScript:https://github.com/friendsofsymfony/fosjsroutingbundle管理Symfony URL。请记住,JavaScript将在树枝之前执行。我确定您显示的错误显示在页面加载中。祝你好运