IM嗨得到它想要的數據DEND我在控制器方法使用AJAX腳本:如何使用後Ajax來發送數據和從symfony的控制器
$("#form_enr").submit(function(ev) {
ev.preventDefault();
var ev = $("#mySelect").val();
var text =$("#form_Text").val();
console.log(text);
console.log(ev);
$.ajax({
type: "POST",
url: "{{ path('invitation_enregistrerInv')}}",
data : 'ev=' + ev + '&text=' + text,
success: function(data){
$('.portlet-body').html(data);
}
});
});
,並得到它,我用這個語法:
public function enregistrerInvAction()
{
$inv = new Invitation();
$em = $this->getDoctrine()->getManager();
$request = $this->get('request');
$ev = $request->request->get('ev');
$text = $request->request->get('text');
$inv->setCreatedAt(new \Datetime());
$inv->setUpdatedAt(new \Datetime());
$inv->setText($text);
$em->persist($inv);
$evenement=$em->getRepository('EvEvenementBundle:Evenement')->findById($ev);
$evenement=new Evenement();
$evenement->setInvitation($inv);
$em->persist($evenement);
$em->flush();
$this->addFlash(
'success',
'Les informations ont été enregistrées!'
);
return $this->redirect($this->generateUrl('invitation_index2'));
}
,但我似乎沒有工作,因爲我有這樣的錯誤:
An exception occurred while executing 'INSERT INTO invitation (created_at, updated_at, text) VALUES (?, ?, ?)' with params ["2016-08-01 17:12:55", "2016-08-01 17:12:55", null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'text' ne peut être vide (null) !!! because text is null , and I don't get it
請使用['Request' injection](http://symfony.com/doc/current/controller.html#the-request-object-as-a-controller-argument)在你的控制器中,容器中的''request''已被棄用['request_stack'] [http://symfony.com/blog/new-in-symfony-2-4-the-request -stack) – NDM
我試過不僅你的建議,但許多其他人,它似乎是一個嚴重的問題 –