doupang3062 2017-08-14 18:16
浏览 133
已采纳

Ajax发送FormData并检索多维数组

Alright so I have a form where you select a CSV file and when you hit the sales_importer button I would like my java to call my php function and get the returned multidimensional array to then do later processing. When I run the following code I get the alert box with a multidimensional array of values but it seems like it is in string form. When I do alert(result[0][0]); I get [ in my alert box.

I have tried changing my dataType in my ajax call to json but then it just fails but I still get a 200 response from my browser. Any suggestions of what might be going on/how to go about fixing it?

js

$('body').on('click', '#sales_importer', function() {
    $.ajax({
        type: 'POST',
        data: new FormData($('form[id="import_form"]')[0]),
        cache: false,
        contentType: false,
        processData: false, 
        url: admin_url+'Clients/import',
        success: function(result){    
            alert(result);                      
        }
    }); 
});

php

public function import()
{
    if ($this->input->is_ajax_request()) {
        if (isset($_FILES['client_file_csv']['name']) && $_FILES['client_file_csv']['name'] != '') {            
            // Get the temp file path
            $tmpFilePath = $_FILES['client_file_csv']['tmp_name'];
            // Make sure we have a filepath
            if (!empty($tmpFilePath) && $tmpFilePath != '') { 
                // Setup our new file path
                $newFilePath = TEMP_FOLDER . $_FILES['client_file_csv']['name'];

                if (!file_exists(TEMP_FOLDER)) {
                    mkdir(TEMP_FOLDER, 777);
                }
                if (move_uploaded_file($tmpFilePath, $newFilePath)) { 
                    $import_result = true;
                    $fd            = fopen($newFilePath, 'r');
                    $rows          = array();
                    while ($row = fgetcsv($fd)) {
                        $rows[] = $row;
                    }
                    $data['total_rows_post'] = count($rows);
                    fclose($fd);                           

                    echo json_encode($rows); 
                }
                unlink($newFilePath);
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • douyan4958 2017-08-14 19:02
    关注

    If you want to be able to use the data in javascript, then you're going to need to apply a key. So in your PHP:

    echo json_encode( array('rows' => $rows) );
    

    This now allows you to access the rows in your jQuery AJAX success function:

    success: function(result){    
        // rows available as result.rows
        if( result.rows ){
            // Do something with result.rows...
            console.log( result.rows );
            $.each( result.rows, function(i, arr){
                 console.log( arr );
            });
        }                     
    }
    

    Remember that result.rows is going to be a javascript object, so you'll need to use jQuery's each to loop through the rows and do what you want to do. Use your console to view result.rows, and you'll see what I mean.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题