symfony2 的ajax问题

qq_36623238 2018-01-17 08:05:46
最近开发php的symfony2 框架时,使用ajax的保存操作时发生错误,有世界上最好的程序员教教吗?

代码如下:
TWIG
function saveData() {
//验证操作
if (!validate()) {
return;
}
var studentInfo = new Object();
var operation = "";
var studentId = $.trim($('#studentId').html());
if (studentId.length == 0) {
create_operate_log("学生信息创建保存操作", "开始");
} else {
create_operate_log("编辑保存ID为" + studentId + "的学生数据", "开始");
}
var name = $.trim($('#studentName').val());
var age = $.trim($('#studentAge').val());
var gender = $.trim($('#studentGender').val());
var score = $.trim($('#studentScore').val());
var version_no = $.trim($('#student_version_no').val());
if (studentId.length == 0) {
operation = "save";
} else {
operation = "update";
}
studentInfo.id = studentId;
studentInfo.name = name;
studentInfo.age = age;
studentInfo.gender = gender;
studentInfo.score = score;
studentInfo.version_no = version_no;
layer.load(2);
$.ajax({
type: "POST",
url: "{{path('tquick_save_student')}}",
data: {"studentInfo": studentInfo},
cache: false,
success: function (data) {
studentInfo.id = data.sid;
//pushToWeixin(newsInfo);
if (data.mes == "success") {
$('#studentId').html(data.sid);
$('#student_version_no').val(data.version_no);
if (operation == "save") {
if (data.isDelbtnDisplay) {
$('#toolBarBtn').append("<button class='btn btn-yellow input-sm' id='newsdetail_btn_delete' onclick='deleteDataConfirm();'>删 除</button>");
}
}
showSuccessInfo("保存成功", "doSearchData(0)");
} else {
showErrorInfo("操作失败", data.mes);
}
layer.closeAll('loading');
},
error: function (data, textStatus) {
showErrorInfo("请求失败", "请求服务失败!");
layer.closeAll('loading');
}
});
if (studentId.length == 0) {
create_operate_log("学生信息创建保存操作", "结束");
} else {
create_operate_log("编辑保存ID为" + studentId + "的学生数据", "结束");
}
}


rounting
////学生信息保存操作
$collection->add('tquick_save_student', new Route('/student/save', array(
'_controller' => getController("Student", "save"),
)));

控制器
/**
* 显示学生信息详细
* @param type $hospitalId
* @return type
*/
public function showStudentDetailAction($studentId = "") {
$studentLogic = new StudentLogic($this->getEntityManager());
$student = $studentLogic->getstudentById($studentId);
return $this->render($this->getView("student:detail"), array("student" => $student));
}

logic
public function saveStudent($studentInfo, $staffId) {
$student = new Student();
$mes = CheckStatusEnum::SUCCESS;
$results = array();
$studentId = $studentInfo["id"];
//当$studentId存在时则做编辑操作
if (!empty($studentId)) {
$student = $this->getEntityById(EntityNameEnum::STUDENT, $studentId);
if (empty($student)) {
$results['mes'] = ErrorMessage::DATA_NOT_EXSIST;
return $results;
}
} else {
//当$studentId不存在时则做新增操作
$student->setVersionNo(CommonConst::VERSION_START);
$this->getEntityManager()->persist($student);
}
$verNo = $student->getVersionNo();
if ($verNo == $studentInfo["version_no"] || empty($studentId)) {
$student->setVersionNo($verNo + CommonConst::VERSION_STEP);
$student->setName($studentInfo["name"]);
$student->setAge($studentInfo["age"]);
$student->setGender($studentInfo["gender"]);
$student->setScore($studentInfo["score"]);
$student->setUpdateStaffId($staffId);
$student->setUpdateDate(new \DateTime);
$this->getEntityManager()->flush();
$results['sid'] = $studentId->getId();
$results['version_no'] = $studentId->getVersionNo();
} else {
$mes = ErrorMessage::ERROR_MSG_DOUBLE_EDIT;
}
$results['mes'] = $mes;
return $results;
}
...全文
604 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧