[英]jquery FormData and Zend Framework 2
我正在嘗試通過FormData(使用jQuery)將數據發送到我的控制器,但是我遇到了數據為空的問題。 我不知道為什么,當我使用console.log()
時,文本會顯示在控制台中,但是當我使用ajax傳遞它時,出現了錯誤,我設置為“狀態文本不能為空”。 我是使用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控制器和操作代碼似乎不錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.