jquery FormData 和 Zend Framework 2
jquery FormData and Zend Framework 2
我正在尝试通过 FormData(使用 jQuery)将数据发送到我的控制器,但我 运行 遇到数据为空的问题。我也不确定为什么,当我使用 console.log()
时,文本显示在控制台中,但是当我使用 ajax 传递它时,我收到了我设置的错误 "status text cannot be empty"。我刚开始使用 FormData,所以如果我对我所说的内容不清楚,我很抱歉。
这是我的代码:
jQuery-
$('#post-status').on('click', function() {
if ($('#status').html() != "Status: " && $('#status').html() != "") {
var status = $('#status').html();
var getstatus;
var getfile;
var formData = new FormData();
if (document.getElementById('status-photo') == null) {
formData.append("statustext", status);
getstatus = formData.get("statustext");
} else {
formData.append("statustext", status);
formData.append("userfile", document.getElementById('status-photo').files[0]);
var getstatus = formData.get("statustext");
var getfile = formData.get("userfile");
}
//console.log(getstatus);
//return false;
$('#status-msg').html("");
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "/members/status/post-status",
dataType: "json",
data: formData
}).done(function(msg) {
$('#status-msg').html(msg.success);
console.log(msg);
$('#status').html('Status: ');
$('#added-status-photos').attr('style', 'display: none;');
$('#delete-include-pic').attr('style', 'display: none;');
$('#include-pic').attr('style', 'display: block;');
// update status text
$.getJSON('/members/status/get-status', function(stat) {
$('#current-status').html("Current Status: " + stat.status);
});
}).fail(function(msg) {
console.log(msg);
$('#status-msg').html(msg.fail);
$('#added-status-photos').attr('style', 'display: none;');
$('#delete-include-pic').attr('style', 'display: none;');
$('#include-pic').attr('style', 'display: block;');
});
} else {
$('#status-msg').html("Please enter a valid status.");
return;
}
});
控制器 -
public function poststatusAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
if ($this->request->isPost()) {
try {
$status = $this->params()->fromPost('status');
$file = $this->params()->fromPost('file');
if (!empty($file)) {
if ($this->getStatusService()->postStatus($status, array('tmp_name' => $_FILES[$file]['tmp_name'],
'name' => $_FILES[$file]['name']))) {
echo json_encode(array('success' => 'Status updated'));
}
} else {
if ($this->getStatusService()->postStatus($status)) {
echo json_encode(array('success' => 'Status updated'));
}
}
} catch (StatusException $e) {
echo json_encode(array('fail' => $e->getMessage()));
}
}
return $view_model;
}
最后是模型 -
public function postStatus($status, array $image = array())
{
if (empty($status)) {
throw new StatusException("Status text cannot be left empty.");
} else {
// get the user's id based on $this->user
$this->select->columns(array('id'))
->where(array('username' => $this->user));
$query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($this->select),
Adapter::QUERY_MODE_EXECUTE
);
if ($query->count() > 0) {
foreach ($query as $result) {
$row = $result;
}
$select = $this->gateway->select(array('id' => $row['id']));
if ($select->count() > 0) {
// update status
$update_data = array(
'status' => $status,
);
$update = $this->gateway->update($update_data, array('id' => $row['id']));
if ($update > 0) {
return true;
} else {
throw new StatusException("Error posting status.");
}
} else {
// insert status
$insert_data = array(
'id' => $row['id'],
'status' => $row['status'],
);
$insert = $this->gateway->insert($insert_data);
if ($insert > 0) {
// put image into status folder (if not null)
if (count($image) > 0) {
if (!@is_dir(getcwd() . '/public/images/profile/' . $this->user . '/status')) {
// make the status directory
// then insert any images if found
mkdir(getcwd() . '/public/images/profile/' . $this->user . '/status');
if (@is_uploaded_file($image['name'])) {
move_uploaded_file($image['tmp_name'], getcwd() . '/public/images/profile/' . $this->user . '/status/' . $image['name']);
return true;
} else {
throw new StatusException("Error uploading your image for your status.");
}
} else {
// insert any images if found
if (@is_uploaded_file($image['name'])) {
move_uploaded_file($image['tmp_name'], getcwd() . '/public/images/profile/' . $this->user . '/status/' . $image['name']);
return true;
} else {
throw new StatusException("Error uploading your image for your status.");
}
}
}
// no image
return true;
} else {
throw new StatusException("Error posting status.");
}
}
} else {
throw new StatusException("Invalid username passed.");
}
}
}
我收到的错误信息在模型的第一行
throw new StatusException("Status text cannot be left empty.");
我附上了两个屏幕截图,希望在我再次不清楚时可能有所帮助
如有任何帮助,我们将不胜感激。
谢谢!
尝试如下更改您的 ajax 代码。
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "/members/status/post-status",
dataType: "json",
data: formData
}).
您的 zend 控制器和操作代码似乎不错。
我正在尝试通过 FormData(使用 jQuery)将数据发送到我的控制器,但我 运行 遇到数据为空的问题。我也不确定为什么,当我使用 console.log()
时,文本显示在控制台中,但是当我使用 ajax 传递它时,我收到了我设置的错误 "status text cannot be empty"。我刚开始使用 FormData,所以如果我对我所说的内容不清楚,我很抱歉。
这是我的代码:
jQuery-
$('#post-status').on('click', function() {
if ($('#status').html() != "Status: " && $('#status').html() != "") {
var status = $('#status').html();
var getstatus;
var getfile;
var formData = new FormData();
if (document.getElementById('status-photo') == null) {
formData.append("statustext", status);
getstatus = formData.get("statustext");
} else {
formData.append("statustext", status);
formData.append("userfile", document.getElementById('status-photo').files[0]);
var getstatus = formData.get("statustext");
var getfile = formData.get("userfile");
}
//console.log(getstatus);
//return false;
$('#status-msg').html("");
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "/members/status/post-status",
dataType: "json",
data: formData
}).done(function(msg) {
$('#status-msg').html(msg.success);
console.log(msg);
$('#status').html('Status: ');
$('#added-status-photos').attr('style', 'display: none;');
$('#delete-include-pic').attr('style', 'display: none;');
$('#include-pic').attr('style', 'display: block;');
// update status text
$.getJSON('/members/status/get-status', function(stat) {
$('#current-status').html("Current Status: " + stat.status);
});
}).fail(function(msg) {
console.log(msg);
$('#status-msg').html(msg.fail);
$('#added-status-photos').attr('style', 'display: none;');
$('#delete-include-pic').attr('style', 'display: none;');
$('#include-pic').attr('style', 'display: block;');
});
} else {
$('#status-msg').html("Please enter a valid status.");
return;
}
});
控制器 -
public function poststatusAction()
{
$layout = $this->layout();
$layout->setTerminal(true);
$view_model = new ViewModel();
$view_model->setTerminal(true);
if ($this->request->isPost()) {
try {
$status = $this->params()->fromPost('status');
$file = $this->params()->fromPost('file');
if (!empty($file)) {
if ($this->getStatusService()->postStatus($status, array('tmp_name' => $_FILES[$file]['tmp_name'],
'name' => $_FILES[$file]['name']))) {
echo json_encode(array('success' => 'Status updated'));
}
} else {
if ($this->getStatusService()->postStatus($status)) {
echo json_encode(array('success' => 'Status updated'));
}
}
} catch (StatusException $e) {
echo json_encode(array('fail' => $e->getMessage()));
}
}
return $view_model;
}
最后是模型 -
public function postStatus($status, array $image = array())
{
if (empty($status)) {
throw new StatusException("Status text cannot be left empty.");
} else {
// get the user's id based on $this->user
$this->select->columns(array('id'))
->where(array('username' => $this->user));
$query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($this->select),
Adapter::QUERY_MODE_EXECUTE
);
if ($query->count() > 0) {
foreach ($query as $result) {
$row = $result;
}
$select = $this->gateway->select(array('id' => $row['id']));
if ($select->count() > 0) {
// update status
$update_data = array(
'status' => $status,
);
$update = $this->gateway->update($update_data, array('id' => $row['id']));
if ($update > 0) {
return true;
} else {
throw new StatusException("Error posting status.");
}
} else {
// insert status
$insert_data = array(
'id' => $row['id'],
'status' => $row['status'],
);
$insert = $this->gateway->insert($insert_data);
if ($insert > 0) {
// put image into status folder (if not null)
if (count($image) > 0) {
if (!@is_dir(getcwd() . '/public/images/profile/' . $this->user . '/status')) {
// make the status directory
// then insert any images if found
mkdir(getcwd() . '/public/images/profile/' . $this->user . '/status');
if (@is_uploaded_file($image['name'])) {
move_uploaded_file($image['tmp_name'], getcwd() . '/public/images/profile/' . $this->user . '/status/' . $image['name']);
return true;
} else {
throw new StatusException("Error uploading your image for your status.");
}
} else {
// insert any images if found
if (@is_uploaded_file($image['name'])) {
move_uploaded_file($image['tmp_name'], getcwd() . '/public/images/profile/' . $this->user . '/status/' . $image['name']);
return true;
} else {
throw new StatusException("Error uploading your image for your status.");
}
}
}
// no image
return true;
} else {
throw new StatusException("Error posting status.");
}
}
} else {
throw new StatusException("Invalid username passed.");
}
}
}
我收到的错误信息在模型的第一行
throw new StatusException("Status text cannot be left empty.");
我附上了两个屏幕截图,希望在我再次不清楚时可能有所帮助
如有任何帮助,我们将不胜感激。
谢谢!
尝试如下更改您的 ajax 代码。
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "/members/status/post-status",
dataType: "json",
data: formData
}).
您的 zend 控制器和操作代码似乎不错。