file upload with error on return json

1

I'm uploading a file with jquery + ajax using the jquery plugin plugin.

The processing of the form is all ok, the file goes up and the data is saved in the database. The problem is in return with the answer in json. This is printing the answer string.

View:

$("#salvar-fcei").click(function () {
        // bind form using ajaxForm 
        $('#jsonForm').ajaxForm({
            // dataType identifies the expected content type of the server response 
            dataType: 'json',
            // success identifies the function to invoke when the server response 
            // has been received 
            success: function (response) {
                if (response.mensagem == 'ok') {
                    $(".modal-title").html("Cadastro realizado com sucesso!");
                    $(".modal-text").html("O gestor foi adicionado com sucesso ao Projeto.");
                    $("#modal").addClass('modal-success');
                    $("#modal").modal();
                } else {
                    $(".modal-title").html("Ops! Verifique os erros abaixo");
                    $(".modal-text").html(response.mensagem);
                    $("#modal").addClass('modal-warning');
                    $("#modal").modal();
                }
            }
        });
    });

The form tag:

<form class="form-horizontal" id="frm-fcei" method="POST" enctype='multipart/form-data' action="<?php echo base_url('projeto/add_fcei'); ?>">

The Controller:

function add_fcei(){
    $response = $this->model_projeto->add_fcei();
    echo json_encode($response);
}

The Model

function add_fcei() {
    $config['upload_path'] = './fcei';
    $config['allowed_types'] = 'pdf';
    $config['file_name'] = 'FCEI-'.$this->input->post('id_projeto').'-'.$this->input->post('numero');

    $this->load->library('upload', $config);

    if ($this->upload->do_upload('arquivo')){
        $data['id_projeto'] = $this->input->post('id_projeto');
        $data['numero'] = $this->input->post('numero');
        $data['dt_emissao'] = $this->input->post('dt_emissao');
        $data['arquivo'] = $this->upload->data('file_name');
        $data['dt_cadastro'] = date('Y-m-d');
        $data['status'] = '1';
        if ($this->db->insert('cadastro_fcei',$data)){
            $response = ['mensagem'=>'ok'];
        } else{
            $response = ['mensagem'=>'Não foi possí­vel cadastrar no banco de dados. Arquivo foi salvo.'];
        }
    } else{
        $response = ['mensagem'=>$this->upload->display_errors()];
    }
    return $response;
}

When I send the form it prints on the array screen

{'mensagem':'ok'}
    
asked by anonymous 28.07.2016 / 21:00

1 answer

1

Set a header for your return.

   function add_fcei(){
        //define um cabeçalho
        header('Content-Type: application/json');
        $response = $this->model_projeto->add_fcei();
        echo json_encode($response);
    }

Remembering that echo in controller is not good practice. The most appropriate would be to create a view for your JSON returns.

    
24.09.2016 / 02:02