doupang3062
doupang3062
2017-08-14 18:16

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 douyan4958 4年前

    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.

    点赞 评论 复制链接分享

为你推荐