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;
});