2014-03-26 81 views
1

在我的Angular/Rails應用中,我使用angular-file-upload gem上傳電子表格。 https://github.com/danialfarid/angular-file-uploadAngular無法訪問返回的數據

我將電子表格傳遞給Rails後端,它處理數據並返回一個JSON數組。

該傳遞文件的向上角碼是這樣的:

$scope.submitForm = function() { 
    console.log('submit upload2'); 
    return $scope.upload = $upload.upload({ 
    url: '/api/batches/spreadsheet_upload.json', 
    data: { 
     id: 99, 
     assembly: "xy" 
    }, 
    file: $scope.file 
    }).success(data, status, headers, config)(function() { 
     debugger 
    $scope.selector.tabledata = data; 
    $scope.uploaded = true; 
    }); 
}; 

導軌接受文件並處理它。我試圖用返回它渲染JSON:{數據:spreadsheet_data}

def spreadsheet_upload 
    Rails.logger.debug("here params: #{params}") 
    spreadsheet = nil 
    case File.extname(params[:file].original_filename) 
    when '.csv' then spreadsheet = Roo::CSV.new(params[:file].path) 
    when '.xls' then spreadsheet = Roo::Excel.new(params[:file].path, nil, :ignore) 
    when '.xlsx' then spreadsheet = Roo::Excelx.new(params[:file].path, nil, :ignore) 
    else raise "Unknown file type: #{file.original_filename}" 
    end 

    spreadsheet_header = spreadsheet.row(1) 
    spreadsheet_data = [] 

    (2..spreadsheet.last_row).each do |i| 
    row = Hash[[spreadsheet_header, spreadsheet.row(i)].transpose] 
    spreadsheet_data.push(row) 
    end 
    render json: {data: spreadsheet_data} 
end 

Chrome瀏覽器中的網絡選項卡顯示我的數據已經從Rails服務器返回,但角度似乎並不知道是什麼與它做。在網絡選項卡

ReferenceError: data is not defined 
    at Object.$scope.submitForm (http://0.0.0.0:3000/assets/controllers/DesignViewCtrl.js?body=1:203:18) 
    at http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:1936:30 
    at http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:4127:15 
    at Object.e.$eval (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:2432:24) 
    at Object.e.$apply (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:2439:40) 
    at HTMLFormElement.<anonymous> (http://0.0.0.0:3000/assets/lib/angular.min.js?body=1:4126:15) 
    at HTMLFormElement.jQuery.event.dispatch (http://0.0.0.0:3000/assets/jquery.js?body=1:2940:98) 
    at HTMLFormElement.elemData.handle (http://0.0.0.0:3000/assets/jquery.js?body=1:2750:123) 

返回數據

{"data":[{"chrom":4.0,"chrom_start":55593607.0,"chrom_end":55593607.0}, {"chrom":"4","chrom_start":55593609.0,"chrom_end":55593617.0},{"chrom":"6","chrom_start":133105152.0,"chrom_end":133105152.0}]} 

我也試圖改變的響應,但是這也沒有工作。

}).success(resp)(function() { 
    $scope.selector.tabledata = resp.data; 
    $scope.uploaded = true; 
    }); 

回答

1

不知道,但在我所見過的success的結構看起來像

.success(function(data, status, headers, config) { $scope.selector.tabledata = data; $scope.uploaded = true; });