我正在尝试使用ajax处理表单和一些额外的数据。
这是我的ajax邮政编码:
NSFetchedResultsController
在这里,我的控制器:
$(document).on('submit', '#edit-entreprise', function (e) {
e.preventDefault();
var $entreprise = $("#liste-entreprises").val();
var $url = Routing.generate('load_edit_entreprise_form');
var $formSerialize = $('#edit-entreprise').serialize() + "&entreprise=" + $entreprise;
$(".panel-entreprise").empty().append('<div class="progress"> <div class="indeterminate"></div> </div>');
$.ajax({
url: $url,
type: 'POST',
data: $formSerialize,
success: function(html) {
console.log(html);
}
});
});
我不知道如何收到表格和额外数据:(
你能解释一下如何做到这一点吗?
谢谢!
答案 0 :(得分:0)
我这样做:
Ajax(我使用FormData):
$('#formId').submit(function (event) {
// Eviter le comportement par défaut (soumettre le formulaire)
event.preventDefault();
var $this = $(this);
$.ajax({
url: $this.attr('action'),
type: $this.attr('method'),
data: new FormData($this[0]),
processData: false,
contentType: false,
error: function (request, status, error) {
callback(request.responseText);
},
complete: function () {
//
},
statusCode: {
//traitement en cas de succès
200: function (response) {
var message = response.message;
callback(response, event);
},
412: function (response, event) {
callback(response);
}
}
});
});
在sf2中这里是处理程序:
public function updateAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('testBundle:testEntity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
//envoi des données JSON en front
$response = new JsonResponse();
$response->setStatusCode(200);
//ajout de données éventuelles
$response->setData(array(
'message' => "Ligne buffer updated",
'form' => json_encode($this->getHtmlForm($entity))));
return $response;
} else {
//form non valide
//envoi des données d'erreurs JSON en front
$response = new JsonResponse();
$response->setStatusCode(412);
$response->setData(array(
'form' => json_encode($this->getHtmlForm($entity)),
'message' => $editForm->getErrorsAsString(),
));
return $response;
}
}