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.

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

报告相同问题?

悬赏问题

  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测