我在Symfony中有一个需要通过ajax调用提交的表单。我对代码进行了修改,但它没有在db中保存任何数据,但也没有给出/显示任何错误。
send_dict = {
type: 'POST',
url: $(this).attr('action'),
processData: true,
data: $('#Form').serialize(),
beforeSend: function(request) {alert('before send');},
success: function (data) {alert("success")},
error: function(xhr, textStatus, thrownError) {
alert('Some Thing Went Wrong, Please Refresh and Try Again...');
}
}
$.ajax(send_dict);
public function createAction(Request $request)
{
$user = $this->getUser();
$address = new Addresses();
if($request->isXmlHttpRequest()) {
// Do something...
if ($request->isMethod('POST')) {
$request = $this->get('request');
$permanent_is_present = $request->get('permanent_is_present');
$present_address = $request->get('present_address');
$present_address_country = $request->get('present_address_country');
// Persisting Objects to the Database
if($permanent_is_present==true){
$address->isIsPresent(true);
$address->isIsPermanent(true);
}else{
$address->isIsPresent(true);
}
$address->setUser($user);
$address->setStreet1($present_address);
$address->setCountry($present_address_country);
$address->setState($present_address_state);
$address->setCity($present_address_city);
//exit(\Doctrine\Common\Util\Debug::dump($address));
// Entity Manager To Get Connected with Doctrine
$em = $this->getDoctrine()->getManager();
// Persists the entire objects....
$em->persist($address);
// Flush queries into database
$em->flush();
$output = array();
$response = new Response();
$output[] = array('success' => true);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}else{
return $this->render('AddressBundle:Addresses:new.html.twig');
}
} else {
return $this->redirect($this->generateUrl('address_new'));
}
}
发送前的警报和ajax功能的成功显示。但是数据没有保存在数据库中? symfony新手,也不知道如何跟踪/调试这个?
更新 还有一件事我注意到系统中没有用户登录,并且他们试图进行ajax调用。它正在发送到控制器,因为找不到用户ID所以它没有保存到数据库
答案 0 :(得分:0)
您需要序列化表单数据。然后为您的表单添加Id属性:
data: $('#Form').serialize(),