I can't get my array from my controller to ajax in codeigniter

Viewed 48

I'm trying to get an array from my controller and my response is allways like this
what am i doing wrong?

This is the console.log inside ajax that return these script that i can not understand



if (!window.console) console = {};console.log = console.log || function(){};console.warn = console.warn || function(){};console.error = console.error || function(){};console.info = console.info || function(){};console.debug = console.debug || function(){};</script>[{"matricula":"1","fullname":"aaa,  bbbbbb","dni":"123"},{"matricula":"2","fullname":"dddd, ddddd","dni":"1234"}]




//CONTROLLER FUNCTIONS

      public function uploadExcel(){
            $this->loadUploadConfiguration();            
            if(!$this->upload->do_upload('file'))
               echo $this->upload->display_errors();
            else {
                $path = $this->upload->data()['file_name'];
                $this->getExcelData($path); // I call the next function
            }
        }   

        private function getExcelData($savedPath){
            $profesionals = array();

            $path = Homec::PATH_TO_SAVE.$savedPath;
            $object = PHPExcel_IOFactory::load($path);
            $rowNumber = 2;
            $worksheet = $object->setActiveSheetIndex(0);
                while($worksheet->getCellByColumnAndRow(0,$rowNumber)->getValue() != ""){
                    if($this->isProfesionalAvailable($worksheet->getCellByColumnAndRow(0,$rowNumber)->getValue())){
                          $profesional = array('matricula' => $worksheet->getCellByColumnAndRow(0, $rowNumber)->getValue(), 
                         'fullname' => $worksheet->getCellByColumnAndRow(1, $rowNumber)->getValue(),
                          'dni' => $worksheet->getCellByColumnAndRow(2, $rowNumber)->getValue()
                          );
                          array_push($profesionals,$profesional);
                    }
                    $rowNumber++;
                }
                  echo json_encode($profesionals);
            $this->deleteSavedFile($path);
        }

AJAX
when i try to loop my data, it is a string and not is a json, when i try to change the contenttype to application/json, it return empty


$( '#formUpload' )
  .submit( function( e ) {
    $.ajax( {
      url: 'uploadExcel',
      type: 'POST',
      data: new FormData( this ),
      processData: false,
      contentType: false, //when i change my contentype to appalication/json, my data inside success is empty 
      success: function(data){
                        console.log(data); // console.log return this
<script type="text/javascript">
if (!window.console) console = {};console.log = console.log || function(){};console.warn = console.warn || function(){};console.error = console.error || function(){};console.info = console.info || function(){};console.debug = console.debug || function(){};</script>[{"matricula":"1","fullname":"aaa,  bbbbbb","dni":"123"},{"matricula":"2","fullname":"dddd, ddddd","dni":"1234"}]
                 //operations = JSON.parse(data) // if i do this, it return an error

              }
    } );

    e.preventDefault();
  } );

VIEW


<div class="upload ">
   <form class="form-inline" id="formUpload" method='post' enctype="multipart/form-data" >
      <div class="form-group ">
         <label class="" for="file">Subir excel</label>
         <input class="form-control-file" type='file' name='file'>
      </div>
      <button class="btn  btn-danger" id="btnUpload" >Subir</button>

   </form>

</div> ```
1 Answers
  $this->deleteSavedFile($path);
  echo json_encode($profesionals);die;

Use in this order. Once you echo json data need to exit or else it continue with the other line.

Related