file upload with json return error

Asked

Viewed 94 times

1

I am uploading a file with jquery+ajax using the jquery form plugin.

The form processing is all ok, the file goes up and the data is recorded in the database. The problem is in the return with the json response. It is printing the response 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();
                }
            }
        });
    });

A tag form:

<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 submit the form it prints on the array screen

{'mensagem':'ok'}
  • 1

    Hello Fabrício, could you clarify what you want to do? The output seems to be according to your code, this is $response = ['mensagem'=>'ok'];. What exactly do you want to happen?

1 answer

0


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 the controller is not a good practice. The most appropriate would be to create a view for your JSON returns.

  • The issue was the return of json to the view. Then I managed to solve the problem. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.