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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里