我尝试用Axios张贴表格。但是我不知道为什么,需要2请求发布以将数据存储到数据库。
表格
<button type="submit" class="btn btn-danger btn-raised" @click="editUser();">Save</button>
Axios POST
editUser: function(formData){
var formData = vmUser.toFormData(vmUser.userData);
axios.post("/admin/users/update", formData)
.then( function(response){
vmUser.userData = {
user_id: $("#user_id").val(),
...
}
toFormData方法
toFormData: function(obj){
var form_data = new FormData();
for ( var key in obj ) {
form_data.append(key, obj[key]);
}
return form_data;
},
Phalcon控制器
$data = [
'result' => 'ok',
'error' => '',
'message' => '',
];
$user_id = $this->request->getPost("user_id");
$user = SyswebUsers::findFirstByUserId($user_id);
$user->user_name = $this->request->getPost("user_name");
...
if (!$user->save()) {
$data['error'] = 'error';
$data['message'] = 'Failed!';
}
else
{
$data['message'] = 'Success!';
}
$this->sendJson($data);