Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery FormData和Zend框架2_Jquery_Zend Framework2_Form Data - Fatal编程技术网

jquery FormData和Zend框架2

jquery FormData和Zend框架2,jquery,zend-framework2,form-data,Jquery,Zend Framework2,Form Data,我试图通过带有jQuery的FormData将数据发送到我的控制器,但我遇到了数据为空的问题。我也不知道为什么,当我使用console.log时,文本会显示在控制台中,但是当我用ajax传递它时,我得到了一个错误,我说状态文本不能为空。我不熟悉使用FormData,所以如果我不清楚我在说什么,我很抱歉 这是我的密码: jQuery- $('#post-status').on('click', function() { if ($('#status').html() != "Status:

我试图通过带有jQuery的FormData将数据发送到我的控制器,但我遇到了数据为空的问题。我也不知道为什么,当我使用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.");
        }
    }
}
我收到的错误消息位于模型的第一行

抛出新的StatusExceptionStatus文本不能为空

我已经包括了两个截图,希望在我不清楚的时候能有所帮助

任何帮助都将不胜感激


谢谢

尝试如下更改ajax代码

$.ajax({
            type: "POST",
            contentType: false,
            processData: false,
            url: "/members/status/post-status",
            dataType: "json",
            data: formData
        }).

您的zend控制器和操作代码似乎很好。

所以将contentType设置为true,将processData设置为true?@user2101411您可以将其注释掉或删除,谢谢,我会给您一个提示go@user2101411我已经更新了我的答案,你只需要发送数据:formData。我之前搞错了,很抱歉。我希望这行得通。谢谢。现在我唯一的问题是,既然我在控制器中使用$this->params->fromPost获得状态和文件,那么我如何获得传递的参数,因为它仍然说文本不能为空错误,但我认为是因为这个原因?