【问题标题】:I can't get my array from my controller to ajax in codeigniter我无法将我的数组从我的控制器获取到 codeigniter 中的 ajax
【发布时间】:2021-04-01 17:08:06
【问题描述】:

我正在尝试从我的控制器中获取一个数组,但我的响应总是这样
我做错了什么?

这是 ajax 中的 console.log,它返回这些我无法理解的脚本



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
当我尝试循环我的数据时,它是一个字符串而不是 json,当我尝试将内容类型更改为 application/json 时,它返回空


$( '#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();
  } );

查看


<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> ```

【问题讨论】:

  • 对不起,我是新来的,我也做错了
  • 为什么你的JS中间有&lt;script...&gt;标签? if (!window.console) ... 是什么东西?删除所有这些 - 只留下 console.log(data),编辑您的问题并粘贴结果。

标签: javascript php jquery ajax codeigniter


【解决方案1】:
  $this->deleteSavedFile($path);
  echo json_encode($profesionals);die;

按此顺序使用。一旦你回显 json 数据需要退出,否则它会继续另一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2018-06-05
    • 2016-06-25
    • 2022-01-05
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多